Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need Mongoose or Mongojs for Node.js?

Tags:

Major Questions

Currently I'm very confused, the main questions are: What is the use of Mongoose/Mongojs? What would be different if I run Node.js without Mongoose/Mongojs?

I can't find any good explanation of the big picture of how Mongoose or Mongojs fits together with Node.js.

Mongoose's slogan: "elegant mongodb object modeling for node.js" is not helping.

Minor Questions

Btw, I think Mongoose and Mongojs are doing the same type of thing for Node.js (i.e. interchangeable), but this page seems to be describing Mongojs quite differently from Mongoose: "Simple driver that emulates the mongodb API as much as possible." So I might be wrong in putting Mongoose and Mongojs together. If so, please clarify as well.


Maybe I shouldn't use Mongojs since stackoverflow doesn't even have a tag for it. If so, just ignore everything I said above about Mongojs.


While on the topic of giving the big picture of Node.js with Mongoose and Mongojs, what else on this page should I know?

Thanks.

like image 253
HaoQi Li Avatar asked May 04 '13 23:05

HaoQi Li


People also ask

Why Mongoose is used in Node JS?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. MongoDB is a schema-less NoSQL document database.

Is it mandatory to use Mongoose with Node application?

It's not mandatory to use Mongoose over the MongoDB Native API. However, there are some benefits to doing so.

Is it necessary to use Mongoose?

Pros/Cons of using Mongoose: Biggest Pro is that it has the data validation built into it(requirements of what data you will allow to be added or to update your database). It will take some work to build that yourself. (but not THAT hard) It will abstract away most of the mongoDB code from the rest of the application.

Should I use MongoDB driver or Mongoose?

On the downside, learning mongoose can take some time, and has some limitations in handling schemas that are quite complex. However, if your collection schema is unpredictable, or you want a Mongo-shell like experience inside Node. js, then go ahead and use the mongodb driver. It is the simplest to pick up.


1 Answers

Luis Elizondo said:

Quick post. I’ve been using Node.js a lot, to be honest, I love it, along with Node, I’ve been using MongoDB which, as you may know if you read my last post, doesn’t support joins.

My first choice on a project was to use mongojs because it was plain simple. It uses the same structure you use on MongoDB directly with a very few exceptions, the main one is the ability to pass a callback as an argument.

I moved really fast with mongojs, but then I hit a wall. Joins. Is just not possible to do it with MongoJS for the same reason you can’t do it with MongoDB, you have to use some sort of black magic to do them, and this is when Mongoose excells.

To be honest, I tried to avoid Mongoose because I thought it was hard to use, defining a model was just “a waste of time” when with MongoJS you don’t need to. I was wrong.

Bottom line. If your project is really simple, no joins, no complicated features, go with MongoJS, is really easy but limited. If you’re trying to save the world with your crazy idea and you need more powers than Superman, spend some time learning Mongoose and use it, it will take you there.

Trust me, I just spent two days rewriting everything using Mongoose, I wish someone told me before.

http://luiselizondo.net/blogs/luis-elizondo/mongoose-vs-mongojs-nodejs

like image 82
Luckylooke Avatar answered Dec 03 '22 03:12

Luckylooke