Book Image

Mongoose for Application Development

By : Simon Holmes
Book Image

Mongoose for Application Development

By: Simon Holmes

Overview of this book

Mongoose is all about putting the data model where it should be: in your application. You can control everything from within your application in JavaScript, eliminating the need to work with the database or a separate management system. Mongoose for Application Development is a practical, hands-on guide that takes you from installing the technology stack through the steps of developing a web application. It covers the key features of Mongoose and how to use them to rapidly develop a Node.js and MongoDB application. This book introduces the full technology stack of Node.js, MongoDB, Express, and Mongoose. It will take you through the process of building an application on this stack with a focus on how Mongoose makes the process quicker and easier. You will see how Mongoose removes a layer of complexity when dealing with MongoDB whilst giving you more control over your data from your application. You will learn how to define schemas and models for your data in JavaScript. Using these schemas and models, you will learn how to build the cornerstone of any web application that will include CRUD operations (creating, reading, updating, and deleting data). If you want to learn how to build applications quickly and efficiently using Node.js, then Mongoose and this book are ideal for you. Using practical examples throughout, Mongoose for Application Development not only teaches you about the concepts of Mongoose, but walks through how to use them to build a real-life application.
Table of Contents (18 chapters)
Mongoose for Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.