monk vs mongoose for Mongodb
Asked Answered
J

3

66

I'm learning NodeJs.

To connect to and use MongoDB from NodeJS, I see a lot of examples using either Monk or Mongoose.

Are these two libraries equivalent ? Do they have the same features or do they each have a specific purpose ?

As a beginner with NodeJS, which should I use ?

Here are some examples of code that uses Monk :

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodejsapp');

----
exports.userlist = function(db) {
    return function(req, res) {
        var collection = db.get('users');
        collection.find({},{},function(e,docs){
            res.render('userlist', {
                "userlist" : docs
            });
        });
    };
};

and here a sample that uses Mongoose :

   var mongoose = require('mongoose');
----
 mongoose.connect('localhost', 'test');
 var db = mongoose.connection;
  db.on('error', console.error.bind(console, 'connection error:'));
  db.once('open', function callback() {
   console.log('Connected to DB');
});

// User Schema
var userSchema = mongoose.Schema({
   username: { type: String, required: true, unique: true },
   email: { type: String, required: true, unique: true },
  password: { type: String, required: true},
});
Jameljamerson answered 12/5, 2014 at 17:36 Comment(0)
E
79

are they same thing do the same connection ? or do they have specific purpose ?

They are different, although they are two approaches to the same basic problem. Mongoose is a quite sophisticated full-on ORM. More features, but more complexity. Monk is smaller in scope and thus easier to understand.

My suggestion is start coding with the basic mongodb driver module directly. When you understand how that works, and how parts of it are annoying, you will understand the benefit of monk and can try that out to see if you like it. I wouldn't recommend mongoose to a beginner. Mongodb is already tricky enough to learn and while mongoose can be helpful, it's API is quite magical and assumes you already know the tricky aspects of mongodb.

Emia answered 12/5, 2014 at 18:10 Comment(2)
I started with Mongoose and didn't have a problem. Their website had excellent documentation and examples of basic CRUD.Briareus
i recommend monk for the beginners, understanding monk documentation is easy quickThirtytwomo
A
29

If you are learning NodeJS and Mongo, you really dont need anything else -- in fact, MongoDB offers a free online class for MongoDB and NodeJS developers. No need for any additional wrappers.

See https://university.mongodb.com/

Apospory answered 14/5, 2014 at 15:28 Comment(0)
M
0

There is a new ORM called mongo-strict

Mongo Strict is a TypeScript-based smart MongoDB ORM that makes the usage of MongoDB safer, easier, and faster, with improved performance. Mongo-strict provides the safety of SQL databases while retaining the flexibility and ease of use of MongoDB. mongo-strict MongoDB ORM for NodeJS

You can give it a try https://www.npmjs.com/package/mongo-strict

Malacostracan answered 21/5, 2023 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.