2.2.6 • Published 5 years ago

just-mongo v2.2.6

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

Just Mongo 2.2

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

Speed ➵

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

TestJust-Mongo 2Mongoose
Connection446
Insert (5k docs)3663686
Find610

...time in ms.

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

Feature compatibility

Just-Mongo 2 has new possibilities of mongodb. Please, note this:

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

ParameterTypeRequriedDefault
modelsobjectno-
logfalse, true, error, warn, info, verbose, debug, sillynofalse
dbstringyes''
hoststringnolocalhost
userstringno''
passwordstringno''
portnumber/stringno27017
  • 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

ParameterTypeRequriedDefault
documentobject/list\<object>yes-
optionsobjectnonull
// insert one document
await Users.insert({ user_id: 1 }, { serializeFunctions: true });
// insert several documents
await Users.insert([
  { user_id: 1 }, 
  { user_id: 2 }
]);

Update

ParameterTypeRequriedDefault
filterobjectyes-
documentobjectyes-
optionsobjectnonull
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

ParameterTypeRequriedDefault
filterobjectyes-
optionsobjectnonull
await Users.deleteOne({ first_name: 'Anton' }, { w: 1 });
await Users.deleteMany({ age: 10 }, { wtimeout: 25 });

Find/Count

ParameterTypeRequriedDefault
filterobjectyes-
optionsobjectnonull
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
ParameterTypeRequriedDefault
filterobjectnonull
countnumberno5
optionsobjectnonull

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.

2.2.6

5 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.8

6 years ago

2.1.7

6 years ago

2.1.6

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.7.9

6 years ago

1.7.8

6 years ago

1.7.4

6 years ago

1.7.3

6 years ago

1.7.2

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.9

6 years ago

1.6.8

6 years ago

1.6.7

6 years ago

1.6.6

6 years ago

1.6.5

6 years ago

1.6.4

6 years ago

1.6.3

6 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago