Mongoose for Application Development

Mongoose for Application Development

Read it now on the O’Reilly learning platform with a 10-day free trial.

O’Reilly members get unlimited access to books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

Book description

Mongoose streamlines application development on the Node.js stack and this book is the ideal guide to both the concepts and practical application. From connecting to a database to re-usable plugins, it’s all here.

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.

Show and hide more Table of contents Product information

Table of contents

  1. Mongoose for Application Development
    1. Table of Contents
    2. Mongoose for Application Development
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
          1. The technology stack – Node.js, npm, MongoDB, and Express
            1. The language and the server – JavaScript and Node
            2. Single-threaded versus multithreaded
              1. Blocking versus non-blocking code
                1. JavaScript callbacks
                2. Running the callback
                3. A Node.js example
                1. What is Mongoose good for?
                2. What Mongoose is not ideally suited for
                3. The cornerstones of Mongoose
                  1. Mongoose schemas
                  2. Mongoose models
                  1. Installing the prerequisites
                  2. Installing Node.js
                  3. Installing npm
                  4. Installing MongoDB
                  5. Installing Express.js
                  6. Installing Mongoose
                    1. Direct installation into project
                    2. Using project dependencies – package.json
                    1. Mongoose default connection
                      1. Using multiple connections
                      1. Setting the port
                      2. Specifying a database user
                      1. Calling the close command
                      2. Closing when the Node process ends
                      1. Creating the connection
                      2. Catching the events
                      3. Opening the connection at application start
                      4. Creating the database
                      1. Introducing schemas
                        1. Field sizes
                        1. String
                        2. Number
                        3. Date
                        4. Boolean
                        5. Buffer
                        6. ObjectId
                        7. Mixed
                          1. Tracking changes to Mixed type
                          1. Warning – array defined as mixed type
                          1. Modifying an existing schema
                          2. Setting a default value
                          3. Only allowing unique entries
                          4. Our final User schema
                          5. What's that "__v" thing?
                            1. Why is this needed?
                            1. Improving the Project schema
                            1. Instances
                              1. Interacting with instances
                              2. Finding a single instance
                              3. Finding many instances
                              1. Overriding the collection name in the schema
                              2. Overriding the collection name in the model
                              3. Building models from our schemas
                              1. Model methods and instance methods
                              2. Setting up the project
                                1. Code structure
                                  1. Adding the routes files
                                  2. Tying the routes to Mongoose models
                                  1. Routes for user management
                                  2. Routes for project management
                                  1. Creating an instance
                                    1. Adding data to the instance
                                    1. Using the saved data
                                    1. Chaining methods
                                    2. The Model.create() method
                                    1. Adding a new user form
                                      1. Adding the Jade template
                                      2. Linking the view to the URL
                                      1. Error trapping
                                      2. Creating a user session
                                      1. Routes
                                      2. New files and functions
                                      1. Approaches to find and read data
                                        1. Using the QueryBuilder
                                        2. Single query operation
                                        3. Static helper methods – finding data
                                        1. findOne() – finding a single user
                                          1. Login form
                                          2. Login action
                                          3. Housekeeping – adding homepage links
                                          4. Try it out!
                                          1. Creating a new static find method
                                          2. Setting up the route
                                          3. Updating the view
                                          4. Building the AJAX call
                                          5. Try it out!
                                          1. Route setup
                                          2. Creating the view
                                          1. Model helper methods
                                            1. Building the commands
                                            2. Which method to choose
                                            3. The catch
                                            1. Tracking user login
                                            2. Editing the current user
                                              1. Routes in use
                                              2. Setting up the form
                                              3. Setting up the controllers
                                              4. Committing the edit
                                              1. Routes
                                              2. New files and functions
                                              1. Deleting data
                                              2. CRUD – deleting user and projects
                                                1. The "Are you sure" page
                                                2. Deleting the user
                                                  1. Improving on this
                                                  1. Routes
                                                  2. New files and functions
                                                  1. Mongoose validation – the basics
                                                    1. Default validators
                                                      1. All SchemaTypes
                                                      2. Number SchemaType
                                                      3. String SchemaType
                                                      1. Single function – no custom error message
                                                      2. Returning a custom error message
                                                        1. Validating a regular expression
                                                        2. Taking the messages out of the schema
                                                        1. Population – references to other collections
                                                          1. Defining data for population
                                                          2. Saving population references
                                                          3. Retrieving populated data
                                                            1. Querying to return a subset of results
                                                            2. Populating into multiple parent items
                                                            1. Creating subdocuments
                                                              1. Saving and validation
                                                              1. Accessing a specific subdocument
                                                              1. Reusable schema plugins
                                                                1. Creating a schema plugin
                                                                  1. Applying the plugin to an existing schema
                                                                  2. Using an external file
                                                                  3. Using schema middleware
                                                                    1. Not just for plugins
                                                                    Show and hide more

                                                                    Product information

                                                                    • Title: Mongoose for Application Development
                                                                    • Author(s): Simon Holmes
                                                                    • Release date: August 2013
                                                                    • Publisher(s): Packt Publishing
                                                                    • ISBN: 9781782168195