ES6 generator-based MongoDB ODM. Fork of vdemedes/mongorito with power-users/business-users in mind.
Last updated a year ago by mrus .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install mongoritwo 
SYNC missed versions from official npm registry.


Build Status Coverage Status

ES6 generator-based MongoDB ODM. Fork of vdemedes/mongorito with power-users/business-users in mind.

Uses official mongodb driver under the hood.

Quick overview

const mongoritwo = require('mongoritwo');
const Model = mongoritwo.Model;

class Post extends Model {


let post = new Post({
	title: 'Steve Angello rocks',
	author: {
		name: 'Emma'
// post saved


  • Based on Promises, which means no callbacks
  • Established API you've already used to
  • Hooks (before:save, around:create, after:remove, etc)
  • Fully covered by tests
  • Using this module results in a beautiful code


$ npm install mongoritwo --save



Check out connection example.

Here's how to connect to a blog database on localhost:

await mongoritwo.connect('localhost/blog');

To disconnect, use mongoritwo.disconnect():

await mongoritwo.disconnect();


Use classes to define models:

const Model = mongoritwo.Model;

class Post extends Model {

This defines model Post with documents in posts collection. To use a custom collection, add collection() method, which returns the name of the desired collection:

class Post extends Model {
	collection () {
		return 'super_cool_posts';

Mongoritwo models can also be defined old-fashioned Backbone way:

const Post = Model.extend({
	collection: 'posts'

Note: collection property has to be set.


Check out attributes example.

To create a new instance of a model, simply use new:

let post = new Post({
	title: 'Great title',
	author: {
		name: 'Emma'

To retrieve a specific attribute (even a nested one):

let title = post.get('title');
let author = post.get('');

All attributes can be retrieved at once using either toJSON() or get():

let attrs = post.toJSON();
let attrs = post.get();

Set new values via set() method:

post.set('title', 'New title');
post.set('', 'Rachel');

Save & Remove

Check out manage example.

Use a save() method to create/update (Mongoritwo handles that for you) a model:

let post = new Post();

await; // creates a new post

post.set('title', 'New title');
await; // updates an existing post

To remove a model from collection:

await post.remove();

You can also remove all models matching a certain criteria:

await Post.remove({ title: 'New title' });


Find all

To fetch all models find() or all() can be used (they're identical):


Find one

Post.findOne({ title: 'New title' });

Find by ID


Find where value equals

Post.where('title', 'New title').find();
Post.where('', 'Emma').find();

Find where value matches a regular expression

Post.where('title', /something/i).find();

Find where attribute exists


Find where value is less/greater than

Post.where('comments_number').lt(5).find(); // less than 5
Post.where('comments_number').lte(5).find(); // less than or equal 5
Post.where('comments_number').gt(5).find(); // greater than 5
Post.where('comments_number').gte(5).find(); // greater than or equal 5

Find where value is one of'comments_number', [4, 8]).find();

Find using OR

Find all models where title equals either "First" or "Second":

Post.or({ title: 'First' }, { title: 'Second' }).find();

Find and modify

Update and retrieve the contents for one single document:

Post.findAndModify({ 'title': 'Unknown' }, [['_id', 'asc']], { '$set': { 'title': 'Found and modified!' } }, { 'new': true });

Limit results


Skip results

Skip first N results:


Sort results

// descending
Post.sort('comments_number', -1);

// ascending
Post.sort('comments_number', 1);

Count results

Count all documents in collection:


Count all documents matching a certain criteria:

Post.count({ awesome: true });


$ npm test


Mongoritwo is released under the MIT License.

Maintainers (1)
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 9
Dependencies (13)
Dev Dependencies (5)
Dependents (1)

Copyright 2014 - 2017 © |