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.
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.
All you need for this book is a computer capable of running Node.js, administrator/installation permissions, and a text editor.
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.
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.".
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 <[email protected]>
, 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.
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.
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.
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 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 <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.