Welcome to weblogs.com.pk Sign in | Join | Help

MongoDB and Mongoose

Lets complete our RESTful api with Express using the database; MongoDB is the widely used database engine in Node world given its also a part of MEAN stack! Once its installed; you run its “daemon” using >mongod from installation-folder/bin; the command prompt needs to remain opened; there is also a way to setup MongoDB as Windows Service. You will also need to create c:\data\db folder where MongoDB store the data. >mongo is its command line shell from where you can query the database engine. To use MongoDB from Express; we need to install mongoose that gives us an API within Node to access and work with MongoDB


  • To install mongoose; we are specifying dash dash save so its dependency gets added into our package.json
  • Given our Express based API is for Ember front-end; we also need “after” module; more details ahead; so I did npm install after –save for that as well

“Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment”; to use it; we create model classes using its “Schema API”; think of it as a Table from relational database world. In the Express app; create models/model.js file that will contain these Schema model definitions and add mongoose and our model file in app.js and using Mongoose’ connect() api open a connection to the MongoDB that will get used across the app!


  • Notice that using Schema api; we are defining “types” of our model using Mongoose’ SchemaTypes
  • Notice the connection string being used in connect() call; the name of our “database” is invoices

With the above arrangements in place; we can start using MongoDB; we can create “initial” data in the database from the shell; but its convenient if we write the code in our Express app instead; so we call it when/where required; lets add the new function “init” in our RESTful api for that


  • Notice the use of connection.db.dropDatabase() call; its done so that even if we call “init” api repeatedly our database remains in the “known initial” state
  • Notice how “Invoice” and “Item” variables are declared using Mongoose’ API and being used later for querying Mongoose; save() calls in this case; the save() also takes a callback; but for our simple case its not being used

Lets finish off by writing out code for remaining api functions; /invoices is interesting; as we are creating the api for Ember app and its default adapter expects the data in JSONAPI.org and for that we need to go one mile extra. For this; we need “after” thats synchronizing the nested queries and their callbacks, we are using to execute all the queries and waiting for their respective callbacks to complete so “after” all these our data is ready to send to the client!


Given we are sending all the invoices and their children items data in the single go; we dont need GET of /invoices/ID and GET of /itemsID


  • Notice how sentItem is retrieved from request’ body.data; we don't need to de-serialize and its already taken care due to body Parser middleware that we already have configured
  • Notice we are using Mongoose’ findOne() api to query the item from the database and then updating its parameters and saving it back
  • Note MongoDB is storing primary key as _id field and Ember front end is sending primary key of the object in id field
  • Notice also that sentItem from Ember app has detailed attributes in item.attributes as per JSONAPI.org format

Further Readings


Known Issues

  • Ember CLI based project comes with live reload; most code changes while Ember Server is running are picked; this is not the case with Express Creator created project and you will need to restart the server yourself if you make any changes in the code
Published Monday, April 11, 2016 11:37 PM by khurram
Filed under: ,


No Comments

New Comments to this post are disabled