just-mongo
v2.2.6
Published
Just using mongoDB
Downloads
77
Readme
Just Mongo 2.2
Simple and fast wrapper for MongoDB.
💪 Motivation
Less code and more action.
Just Mongo allows you to use a simplified API. Inside, we try to use minimalistic solutions that do not steal your time.
You choose the possibilities of the library, and nothing more.
Speed ➵
We ran along with Mongoose, and overtook him in all the races!
| Test | Just-Mongo 2 | Mongoose | |:-----|:----:| :-------:| | Connection | 4 | 46 | | Insert (5k docs) | 366 | 3686 | | Find | 6 | 10 |
...time in ms.
All tests are in directory: test/speed-test.
Feature compatibility
Just-Mongo 2 has new possibilities of mongodb. Please, note this:
https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/
https://docs.mongodb.com/manual/core/schema-validation/#json-schema
Switching from v1.x
The following should be know when upgrading version:
- Better, update old models to new json schemes.
- Old models will work in limited functionality. Stop working:
isValid
.
Install
$ npm i just-mongo -S
Tests
$ npm test
Docs
Create models
Limited version:
const models = {
users: {
name: {
type: String,
},
age: Number,
id: {
type: Number,
required: true
},
ban: {
type: Boolean,
default: false
}
}
};
Flexible version:
const models = {
users: {
$jsonSchema: {
bsonType: 'object',
properties: {
name: {
type: 'string'
},
age: {
type: 'number'
},
id: {
type: 'number'
},
ban: {
type: 'boolean',
default: false
}
},
required: ['name', 'id']
}
}
};
The default values are not supported by the database itself, the values are set before creating the records. Do not use the default values in schemes in
allOf
,anyOf
,oneOf
, .etc.
Create connection
| Parameter | Type | Requried | Default |
|:----------|:----:| :-------:| :------:|
| models | object | no | - |
| log | false
, true
, error
, warn
, info
, verbose
, debug
, silly
| no | false |
| db | string | yes | '' |
| host | string | no | localhost |
| user | string | no | '' |
| password | string | no | '' |
| port | number/string | no | 27017 |
- log — Set the logging.
import JustMongo from 'just-mongo'
// const JustMongo = require('just-mongo').default;
const mongo = new JustMongo({
models,
db: 'database'
}, (err, done) => {
if (err) {
console.error(err)
} else {
console.log(done)
}
});
If you need create multi connections, read this doc.
Collection
const Users = mongo.collection('users');
Collection [native]
const Users = mongo.collection('users').collection;
Such a method should be used if you are sure that there is already a connection to the MongoBD. If there is no such certainty, then use the method described below.
const Users = mongo.collection('users');
await Users.native((collection, resolve, reject) => {
// use collection.MethodFromNative
// сomplete the function using resolve or reject
});
This method will be executed after connecting to the database. After that, you can use the first method.
Insert
| Parameter | Type | Requried | Default | |:----------|:----:| :-------:| :------:| | document | object/list<object> | yes | - | | options | object | no | null |
// insert one document
await Users.insert({ user_id: 1 }, { serializeFunctions: true });
// insert several documents
await Users.insert([
{ user_id: 1 },
{ user_id: 2 }
]);
Update
| Parameter | Type | Requried | Default | |:----------|:----:| :-------:| :------:| | filter | object | yes | - | | document | object | yes | - | | options | object | no | null |
await Users.updateOne({ user_id: 1 }, {
$set: {
first_name: 'Mikhail'
}
}, { serializeFunctions: false });
await Users.updateMany({ first_name: 'Mikhail' }, {
$set: {
age: 15
}
}, { w: 1 });
Or use methods editOne and editMany to avoid specifying $set for each request.
Delete
| Parameter | Type | Requried | Default | |:----------|:----:| :-------:| :------:| | filter | object | yes | - | | options | object | no | null |
await Users.deleteOne({ first_name: 'Anton' }, { w: 1 });
await Users.deleteMany({ age: 10 }, { wtimeout: 25 });
Find/Count
| Parameter | Type | Requried | Default | |:----------|:----:| :-------:| :------:| | filter | object | yes | - | | options | object | no | null |
const item = await Users.findOne({ age: 15 }, { limit: 5 });
const items = await Users.find({});
const itemsCount = await Users.count({ age: 15 }, { maxTimeMS: 2500 });
You can configure additional logic for the contents of documents.
Searching for random entries
| Parameter | Type | Requried | Default | |:----------|:----:| :-------:| :------:| | filter | object | no | null | | count | number | no | 5 | | options | object | no | null |
Options:
- project — control the display of fields as a result.
const items = await Users.findRandom({ age: 25 }, 2, {
project: {
id: 1,
name: 1,
_id: 0
}
});
Getting updates
To constantly receive new data from one or more collections at once, you can use our listening solution. Open doc.
Join engine
If you need joined collection, use join engine.
There's some cool examples too.
Native connections
In case you need to create your own flexible connection using mongodb native, read this doc.
License
MIT.