# just-mongo

> Just using mongoDB

Latest version **2.2.6** (published 2019-10-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install just-mongo
pnpm add just-mongo
yarn add just-mongo
bun add just-mongo
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.6 |
| Published | 2019-10-13 |
| First published | 2017-11-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 7.0.0 |
| Dependencies | 4 |
| Unpacked size | 90.9 KB |
| Known vulnerabilities | 0 (+8 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Anton Danilov |
| Maintainers | deviun |
| Keywords | mongo, mongodb |

## Links

- npm: https://www.npmjs.com/package/just-mongo
- Repository: https://github.com/deviun/just-mongo
- Homepage: https://github.com/deviun/just-mongo#readme
- Issues: https://github.com/deviun/just-mongo/issues
- npm.io page: https://npm.io/package/just-mongo

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) 4.17.11
- [mongodb](https://npm.io/package/mongodb.md) 2.2.33
- [winston](https://npm.io/package/winston.md) 2.4.0
- [bluebird](https://npm.io/package/bluebird.md) 3.5.1

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 2.2.6 (latest) — 2019-10-13
- 2.2.5 — 2019-03-16
- 2.2.4 — 2019-03-16
- 2.2.3 — 2019-03-09
- 2.2.2 — 2019-03-09
- 2.2.1 — 2019-03-09
- 2.2.0 — 2019-02-22
- 2.1.8 — 2018-08-15
- 2.1.7 — 2018-08-07
- 2.1.6 — 2018-08-02
- 2.1.5 — 2018-07-30
- 2.1.4 — 2018-07-30
- 2.1.3 — 2018-07-02
- 2.1.2 — 2018-05-25
- 2.1.1 — 2018-05-25
- … 32 more at https://npm.io/package/just-mongo/versions

## README

# Just Mongo 2.2
[![just-mongo](https://img.shields.io/npm/v/just-mongo.svg?style=flat-square)](https://www.npmjs.com/package/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](http://mongoosejs.com/), 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](https://github.com/deviun/just-mongo/tree/master/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

```sh
$ npm i just-mongo -S
```

## Tests

```sh
$ npm test
```

--------
## Docs
### Create models

Limited version:
```javascript
const models = {
  users: {
    name: {
      type: String,
    },
    age: Number,
    id: {
      type: Number,
      required: true
    },
    ban: {
      type: Boolean,
      default: false
    }
  }
};
```

Flexible version:

```javascript
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.

```javascript
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](https://github.com/deviun/just-mongo/blob/master/docs/multi-connection.md).

### Collection

```javascript
const Users = mongo.collection('users');
```

### Collection [native]

```javascript
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.

```javascript
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](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#insert)

| Parameter | Type | Requried | Default |
|:----------|:----:| :-------:| :------:|
| document | object/list\<object\> | yes | - |
| options | object | no | null |

```javascript
// insert one document
await Users.insert({ user_id: 1 }, { serializeFunctions: true });
// insert several documents
await Users.insert([
  { user_id: 1 }, 
  { user_id: 2 }
]);
```

### [Update](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#update)

| Parameter | Type | Requried | Default |
|:----------|:----:| :-------:| :------:|
| filter | object | yes | - |
| document | object | yes | - |
| options | object | no | null |

```javascript
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](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#deleteMany)

| Parameter | Type | Requried | Default |
|:----------|:----:| :-------:| :------:|
| filter | object | yes | - |
| options | object | no | null |

```javascript
await Users.deleteOne({ first_name: 'Anton' }, { w: 1 });
await Users.deleteMany({ age: 10 }, { wtimeout: 25 });
```

### [Find/Count](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#find)

| Parameter | Type | Requried | Default |
|:----------|:----:| :-------:| :------:|
| filter | object | yes | - |
| options | object | no | null |

```javascript
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](https://github.com/deviun/just-mongo/tree/master/docs/document-project.md) 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.

```javascript
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](https://github.com/deviun/just-mongo/tree/master/docs/listen-collection.md).


### Join engine

If you need joined collection, use [join engine](https://github.com/deviun/just-mongo/tree/master/docs/join-engine.md).

---
There's some cool [examples too](https://github.com/deviun/just-mongo/blob/master/examples/jmongo.test.js).

----
### Native connections

In case you need to create your own flexible connection using **mongodb native**, read [this](https://github.com/deviun/just-mongo/blob/master/docs/native-connection.md) doc.

## License

MIT.

---
_Source: https://npm.io/package/just-mongo · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
