How to Get Start with Mongoose

6/22/2014

This article provide information for you to get start with mongoose.

Before you start you must have NPM and Mongo Database install

Add mongoose dependency to node package file

 "dependencies": {
    "express": "~4.0.0",
    "mongoose": "3.8.12"
  },

Run NPM command to install

npm install mongoose

Try!

Try look the following example to write code.

NPM mongoose - NPM Package

http://mongoosejs.com/ - Official mongoose web site.

Best Practice

After you installed and tried sample code, you'll wonder what's the best practise to imprement.

You could referenece following article(it have file to download).

mongoose-connection-best-practice

 

Conclusion

I think the best way is hide all the mongo db connection in one place, which handler connect to mongo, close mongo connection when node.js closed. error hanlder mongo, also defined all the collection.

These should be in one place. So your node.js side just require that file and use function to CRUD.

I'll post a example next time.