What Mongoose is all about
Mongoose is an object modeling tool for MongoDB and Node.js. What this means in practical terms is that you can define your data model in just one place, in your code.
Yes, that's right. You don't have to create a schema in the database, link that to an ORM or map it into your project objects and classes. You can just define your data structure in JSON inside your project.
The first time I created a project like this I was amazed at how much time and frustration it saves. Even now I still get that warm glow when I start a new project or prototype using Mongoose. It's like taking a shortcut to work down deserted country lanes while everybody else is gridlocked on the highway.
A schema definition can be as simple as the following code snippet:
var userSchema = new mongoose.Schema({ firstname: String, lastname: String, createdOn: Date });
A document in MongoDB created from this schema would be like the following code snippet:
{ "__v" : 0, "_id" : ObjectId("51412597e8e6d3e35c000001"),"createdOn" : ISODate("2013-03-14T01:19:19.866Z"),"firstname" : "Simon", " lastname " : "Holmes" }
If you want to refactor, then you can just do it from within your code, saving a huge amount of development time.
What is Mongoose good for?
Mongoose is primarily useful when you want to interact with structured data in MongoDB. It allows you to define a schema for your data, so that you can interact with your MongoDB data in a structured and repeatable way.
Mongoose helps with many common MongoDB tasks, and removes some of levels of complexity from the nested callbacks you find yourself getting lost in with the native MongoDB driver.
Mongoose also returns the data to you as a JSON object that you can use directly, rather than the JSON string returned by MongoDB.
Mongoose also has a whole suite of helper functions and methods that we'll explore throughout the subsequent chapters of this book.
What Mongoose is not ideally suited for
Mongoose is probably not the answer for you if you are primarily working with the following:
Schema-less data
Random documents
Pure Key-Value pairs
The cornerstones of Mongoose
There are two aspects of Mongoose that we need to introduce you to before going much further:
Schemas
Models
This is a very high-level view; so don't worry if you're not 100 percent confident with this just yet, as we'll be covering it in a lot more detail later in this book.
Mongoose schemas
As we saw earlier, a schema is fundamentally describing the data construct of a document. This schema defines the name of each item of data, and the type of data, whether it is a string, number, date, Boolean, and so on.
var userSchema = new mongoose.Schema({ name: String, email: String, createdOn: Date, verified: Boolean });
In most scenarios you would have one schema for each collection within the database.
Schemas are a powerful aspect of Mongoose, which can also be extended with helper functions and additional methods. But we'll describe more about that in a later chapter.
Mongoose models
A model is a compiled version of the schema. One instance of the model will map to one document in the database.
Creating a User
instance based on the schema userSchema
is a one line task:
var User = mongoose.model('User', userSchema);
It is the model that handles the reading, creating, updating, and deleting of documents.