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.
// const JustMongo = require('just-mongo').default; const mongo = models db: 'database' { if err console else console };
If you need create multi connections, read this doc.
Collection
const Users = mongo;
Collection [native]
const Users = mongocollection;
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; await Users;
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 documentawait Users;// insert several documentsawait Users;
Update
Parameter | Type | Requried | Default |
---|---|---|---|
filter | object | yes | - |
document | object | yes | - |
options | object | no | null |
await Users; await Users;
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;await Users;
Find/Count
Parameter | Type | Requried | Default |
---|---|---|---|
filter | object | yes | - |
options | object | no | null |
const item = await Users;const items = await Users;const itemsCount = await Users;
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;
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.