node package manager
Love JavaScript? Your insights can make it even better. Take the 2017 JavaScript Ecosystem Survey ¬Ľ

just-mongo

Just Mongo

just-mongo

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 superfluous.

Speed ‚ěĶ

We ran along with Mongoose, and overtook him in all the races!

Test Just-Mongo Mongoose
Connection 11 52
Insert (5k docs) 160 4785
Find 3 53

...time in ms.

All tests are in directory: test/speed-test.

Install

$ npm i -S just-mongo

Tests

$ npm test

Docs

Create models

const models = {
  users: {
    name: {
      type: String,
      isValid: (value) => String(value).split(' ').length === 1
    },
    age: Number,
    id: {
      type: Number,
      required: true
    },
    ban: {
      type: Boolean,
      default: false
    }
  }
};

Data types

Type Data Data without strict
String "you data" [1,2,3] ‚üĻ "1,2,3"
{} ‚üĻ "[object Object]"
5 ‚üĻ "5"
true ‚üĻ "true"
Number 12345 [1,2,3] ‚üĻ NaN
{} ‚üĻ NaN
"5" ‚üĻ 5
"abc" ‚üĻ NaN
true/false ‚üĻ 1/0
Boolean true [1,2,3] ‚üĻ true
{} ‚üĻ true
"5" ‚üĻ true
0 ‚üĻ false
Object {foo: 'bar'} [1,2,3] ‚üĻ [1,2,3]
"abc" ‚üĻ {0: 'a', 1: 'b', ...}
5 ‚üĻ {0: 5}
true ‚üĻ {0: true}
Array [1, 2, 3] notArrayType ‚üĻ [notArrayType]

Note that the Object and Array are different types of data, and if you have a strict enabled, then when the array is checked, the object does not pass the validation.

Create connection

Parameter Type Requried Default
models object yes -
log false, true, error, warn, info, verbose, debug, silly no false
strict boolean no false
db string yes ''
host string no localhost
user string no ''
password string no ''
port number/string no 27017
  • log ‚ÄĒ Set the logging.
  • strict ‚ÄĒ Strict validation mode, if true, then an error occurs if the data type is incorrect. If the mode is disabled, the data will be automatically corrected as much as possible.
const JustMongo = require('just-mongo');
 
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

ISC.