Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

monk vs mongoose for Mongodb

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}, }); 
like image 443
Young Avatar asked May 12 '14 17:05

Young


People also ask

Is mongoose good for MongoDB?

Mongoose, a neat ODM library for MongoDB used in Node. js projects, has plenty of useful features that make developers' lives easier. It manages relationships between data, has schema validation, and overall allows coding 3-5 times faster.

Which is better MongoDB or mongoose?

Mongoose vs MongoDB Node. The benefit of using Mongoose is that we have a schema to work against in our application code and an explicit relationship between our MongoDB documents and the Mongoose models within our application.

Can we use MongoDB without mongoose?

Yes, of course, drivers provide many advantages to make our lives easy. But to keep things simple and lightweight we can use only MongoDB for CRUD operation without a mongoose. What about validation? Don't worry, packages like sanitize-html can be used in MongoDB to validate data before storing to a database.

Is mongoose an ODM or ORM?

Mongoose is an ODM that provides a straightforward and schema-based solution to model your application data on top of MongoDB's native drivers.


2 Answers

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.

like image 57
Peter Lyons Avatar answered Sep 18 '22 12:09

Peter Lyons


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/

like image 40
alernerdev Avatar answered Sep 18 '22 12:09

alernerdev