Using Mongoose


One of the things I really like about MongoDB is its flexibility. Data is schemaless, and so you can insert pretty much any JSON object you like into a MongoDB collection. Having not spent too much time with so-called NoSQL databases, this takes a little bit of getting used to.

One area which can cause problems, however, is if your application relies on data being structured in a certain way. For many apps, this is the case - for example, you may wish to loop through an array of items, displaying them in a list, expecting each item to have certain fields to be rendered in the same layout. For these situations, Mongoose is a big help. Mongoose adds (amongst other things) Schemas for your server models, which makes it easy to enforce data consistency.

But, wait a minute? Isn’t the benefit of a NoSQL database the flexibility of schemaless data models, which Mongoose has just introduced? Aren’t we now moving to a SQL-like rigid structure? Sort of. Mongoose is quite flexible, and you don’t have to use schemas if you don’t want to. So, you can mix and match - use schemas where you want to enforce data consistency and validation, and use a Mixed SchemaType when you don’t want or care.

I’ve been using Mongoose for the past few days, and I am enjoying it. The SQL part of my brain is thankful for schemas but by using the Mixed Schema type I also get the flexibility of a document store. So far, so good.