Mongodb.js vs Mongoose, Why we chose Mongodb.js

NODE.JS AND THE API

For the contract I am currently working on, we are building an API using node.js, express.js and mongodb. For node.js to access data in a Mongodb database,  you need to have an library. There are several ways including the native mongodb driver, Mongoose, Mongolian Dead Beef, MongoSkin and I am sure there are tons I did not mention.

USING MONGOOSE

Mongoose brings schema’s and models to Node.js / Mongodb. Yes, it brings a schema to a NoSQL database. I think it makes no sense to do that. If you wanted models and schemas, go ahead and use MySQL.  Taking a database and putting schemas on top actually defeats the purpose.

One of the complaints I hear is that using Mongodb.js , developers have a problem with the joins and the schema. This is a problem with the developer, not with Mongodb or NoSQL databases in general.

DIFFERENCE BETWEEN THE TWO

Mongoose is higher level interface to Mongodb and actually uses mongodb.js,  the MongoDB driver. The question is not really which one is better or worse, the question for us is

Do the benefits of an ODM in Mongoose outweigh the drawbacks?

If you’re looking for an object modeling (ODM) tool so that you do not have to learn a lot about the way Mongodb works, then Mongoose is probably for you. If you want a fast driver and really get the most out of Mongodb, then use the native driver. We know our way around Mongodb so Mongoose would have slowed use down and our app.

BOTTOM LINE

If you are having a problem using schemas ( or complaining about joins )  in any NoSQL database, it is time to stop and spend a little bit more time understanding NoSQL databases and why they work the way they do.

Some of the things you get from Mongoose, validation, etc are good uses, I just think there are a lot better ways to do it in javascript than wrapping a NoSQL database in schemas.

Advertisements

7 thoughts on “Mongodb.js vs Mongoose, Why we chose Mongodb.js

  1. “If you wanted models and schemas, go ahead and use MySQL. Taking a database and putting schemas on top actually defeats the purpose.”

    Uh, that’s like saying compiler error checking defeats the purpose of writing software. Or wearing a seatbelt defeats the purpose of driving a car. Schemas are there to help you make sure that all your objects are consistent, which is what you want 99% of the time. The ability to have non-uniform documents in a collection is powerful but requires a lot of care and incurs a lot of risk. Having extra structure to prevent you from shooting yourself in the foot is valuable. It definitely doesn’t defeat the purpose of databases.

    • I think you have a SQL mentality toward schemas. NoSQL databases do have schemas, they are enforced in code, not at the database level. This has the benefit of a more robust system ( depending on the application of course )

      Say for example, you have hundreds of home based appliances ( think Internet of Things ) sending data back to a central server for collection and analysis.

      If the appliance starts sending data that does not fit your schema (programming error or data input error), would you rather throw the data away because it does not fit you “schema” or collect the data and find out the problem later. I care about data and I would prefer to keep the data and worry about what format it was in later.

      Sure, in theory we would never make code mistakes or design changes that would necessitate such a thing. In the real world, it happens more than we would like to admit.

  2. Hey, that was awesome! Helped me so much to decide whether to go for Mongoose or MongoDB Driver, but there is one question, instead it is a request; Well, what you think about MongoSkin? (as per my knowledge it does not support schema approach) will it be better to use?
    Thanks!

    • Mongoskin is cool. It was one of the earlier ones. That being said, MongoSkin seems to have died, as it hasn’t been updated in a couple of years.

      IMHO, Maintenance would seem to rule Mongoskin out of consideration for me, I would worry about using it as a base for a project going forward.

      I am talking about the npm mongoskin
      $ npm install mongoskin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s