Node.js with Mongoose

Mongoose was written to provide a developer-friendly layer above the standard MongoDB node.js driver, which saves time for developers who want to get rid of boilerplate code writing and concentrate only on application logic.

Mongoose can be installed through npm (added -g flag to install it globally):

sudo npm install mongoose -g

Defining a Schema in mongoose

Mongoose has the concept of Schema which helps mapping and reading, writing and converting application level data from/to MongoDB. If you define the schema correctly, then data conversion is done automatically. Let's assume you want to write an application that handles bank accounts and transactions. For this, I defined two schemas – one for transactions, and one for bank accounts.

Schema for Bank Account

I created a simple bank account object, with only a couple of fields. I added the fields so I can demonstrate the data types that are supported by Mongoose.

You can define a schema using the mongoose.Schema() method, recieves a JSON object, with the name of the fields and types. The owner, accountNumber, currency, balance, and createdOn fields are self-explanatory.

The transactions field is more interesting since this is defined as an array of transactionSchemas, which is specified in the transaction.js file. This way, Mongoose will enforce the developer to add only models of type transactionSchema to the transactions array.

You can define methods for the new type along with the schema definition. In this case, I defined the displayInfo() method which prints the details of the bank account object and lists all the transactions related to it to the console. You will see later how can this be used.

Models can be created using the mongoose.model() method, wherein the first parameter is the name of the collection that stores the model (this gets pluralized) and the schema. The mongoose.model() returns a constructor object that's specific to the schema. Afterwards, you can use this to create models using the new keyword. I created the myAccount model, set the properties, and added two transactions to the transactions array. Since these were created using transactionSchema, Mongoose should handle it without any issues.

The save() method needs a callback function with three parameters: first is the error object (if there was any), second parameter is the account object which was saved to the database, and the third is a counter, giving information about how many documents were affected by the operation.

Data stored in MongoDB can be seen below. Notice that each document, even the nested ones (like transactions) got an _id field; this was added by mongoose to ensure there is a unique identifier for each object. You can force mongoose to skip adding the _id field to the generated objects using:

Using Mongoose for data manipulation is very convenient because you can concentrate on application logic development and not on writing boilerplate code for creating/opening database connections, writing insert statements, and data conversion logic.

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript.
I like technical writing and have good experience in creating tutorials and how to technical articles.
I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...