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

Preface

Mongoose for Application Development will show you how to leverage the power of Mongoose to dramatically speed up your development process with Node.js and MongoDB. At the highest level, Mongoose is all about having a data model, and bringing control and management of that model into your application. Mongoose enables you to create a robust yet rich data structure, providing you with a level of database management that you don't normally get with MongoDB. With its built-in helper methods, Mongoose provides a lot of the common functionality you will need, while also providing a framework for extending it in ways to meet your application needs.

What this book covers

Chapter 1, Introducing Mongoose to the Technology Stack, takes a look at the Node.js, MongoDB, and Express technology stack and introduces Mongoose and shows where it fits in.

Chapter 2, Establishing a Database Connection, covers the different methods of creating database connections with Mongoose, including how and when to use them.

Chapter 3, Schemas and Models, introduces the two cornerstones of Mongoose, covering how to create them and how they relate to the data.

Chapter 4, Interacting with Data – An Introduction, explains how Mongoose provides methods for easily interacting with data, setting the scene for the following four chapters.

Chapter 5, Interacting with Data – Creation, covers the ways we can use Mongoose to create data and save it to the database.

Chapter 6, Interacting with Data – Reading, Querying, and Finding, covers the ways we can use Mongoose to find the data we want by querying the database and reading the data back into Model instances. It shows how to use the built-in methods, and also how to extend Mongoose to run the specific queries that you may want.

Chapter 7, Interacting with Data – Updating, covers the ways we can use Mongoose to change existing data, including the built-in helper methods, and a more robust approach for data integrity.

Chapter 8, Interacting with Data – Deleting, covers the ways we can use Mongoose to delete documents from the database.

Chapter 9, Validating Data, looks at maintaining data integrity, covering the validators built in to Mongoose and explaining how to add custom validation.

Chapter 10, Complex Schemas, introduces the concepts of population and sub-documents to allow richer data models, mimicking some of the functionality found with traditional SQL JOIN statements.

Chapter 11, Plugins – Reusing Code, introduces the Mongoose plugin architecture, covering how to create your own re-usable plugins to remove repetition in your schemas.

What you need for this book

All you need for this book is a computer capable of running Node.js, administrator/installation permissions, and a text editor.

Who this book is for

This book is for people who are interested in building applications in Node.js. If you want to build applications quickly with a robust and manageable data structure then this book is for you! No experience with Node is necessary, but some basic knowledge of HTML and JavaScript would be useful. The focus of the book is on the power of Mongoose, so experienced Node.js developers will also find it useful.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "You may find that your installation of Express has already created the file routes/user.js—this is fine, you can just open it and delete the contents."

A block of code is set as follows:

var userSchema = new mongoose.Schema({
  name: String,
  email: {type: String, unique:true},
  createdOn: Date,
  modifiedOn: { type: Date, default: Date.now },
  lastLogin: Date

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

var express = require('express')
  , db = require('./model/db')
  , routes = require('./routes')
  , user = require('./routes/user')
  , project = require('./routes/project')

Any command-line input or output is written as follows:

$ sudo apt-get install npm

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "If it finds it, MongoDB (not Mongoose) will return an E11000 error.".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.