Mongoose for Application Development

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.

  • Rapid application development with Mongoose on the Node.js stack
  • Use Mongoose to give structure and manageability to MongoDB data
  • Practical exampless on how to use Mongoose for CRUD operations
  • Provides a number of helpful tips and takes away the complexity of everyday MongoDB operations
  • Walks you through building a project management application using Mongoose, Node.js, MongoDB, and Express

In Detail

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

  1. Mongoose for Application Development
    1. Table of Contents
    2. Mongoose for Application Development
    3. Credits
    4. About the Author
    5. About the Reviewers
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      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
    8. 1. Introducing Mongoose to the Technology Stack
      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
        3. The database – MongoDB
        4. The framework – Express
      2. What Mongoose is all about
        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
      3. Installing the full stack
        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
        7. Creating a project
      4. Summary
    9. 2. Establishing a Database Connection
      1. Mongoose default connection
        1. Using multiple connections
      2. About the connection string
        1. Setting the port
        2. Specifying a database user
      3. Connection options
      4. Closing the connection
        1. Calling the close command
        2. Closing when the Node process ends
      5. Connection events
      6. Connecting our project
        1. Creating the connection
        2. Catching the events
        3. Opening the connection at application start
        4. Creating the database
      7. Summary
    10. 3. Schemas and Models
      1. Introducing schemas
        1. Field sizes
      2. Data types allowed in schemas
        1. String
        2. Number
        3. Date
        4. Boolean
        5. Buffer
        6. ObjectId
        7. Mixed
          1. Tracking changes to Mixed type
        8. Array
          1. Warning – array defined as mixed type
        9. Custom SchemaTypes
      3. Where to write the schemas
      4. Writing a schema
        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?
        6. Defining the Project schema
          1. Improving the Project schema
      5. Building models
        1. Instances
          1. Interacting with instances
          2. Finding a single instance
          3. Finding many instances
        2. Considerations when choosing your model name
      6. Setting the collection name
        1. Overriding the collection name in the schema
        2. Overriding the collection name in the model
        3. Building models from our schemas
      7. Our complete code
      8. Summary
    11. 4. Interacting with Data – an Introduction
      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
        2. URLs and routes
          1. Routes for user management
          2. Routes for project management
      3. Summary
    12. 5. Interacting with Data – Creation
      1. Creating an instance
        1. Adding data to the instance
      2. Saving an instance
        1. Using the saved data
      3. Creating and saving database entry in one step
        1. Chaining methods
        2. The Model.create() method
      4. CRUD – create data
        1. Adding a new user form
          1. Adding the Jade template
          2. Linking the view to the URL
        2. Adding the create user function
          1. Error trapping
          2. Creating a user session
        3. Displaying the confirmation page
        4. Try it out!
        5. Adding create project functionality
          1. Routes
          2. New files and functions
      5. Summary
    13. 6. Interacting with Data – Reading, Querying, and Finding
      1. Approaches to find and read data
        1. Using the QueryBuilder
        2. Single query operation
        3. Static helper methods – finding data
      2. CRUD – reading user and project data
        1. findOne() – finding a single user
          1. Login form
          2. Login action
          3. Housekeeping – adding homepage links
          4. Try it out!
        2. find() – finding a list of projects and returning JSON to AJAX
          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!
        3. findByld() – finding a single project
          1. Route setup
          2. Creating the view
      3. Summary
    14. 7. Interacting with Data – Updating
      1. Model helper methods
        1. Building the commands
        2. Which method to choose
        3. The catch
      2. The three-step find-edit-save approach
      3. CRUD – editing users and projects
        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
        3. Editing projects
          1. Routes
          2. New files and functions
      4. Summary
    15. 8. Interacting with Data – Deleting
      1. Deleting data
      2. CRUD – deleting user and projects
        1. The "Are you sure" page
        2. Deleting the user
          1. Improving on this
        3. Deleting individual projects
          1. Routes
          2. New files and functions
      3. Summary
    16. 9. Validating Data
      1. Mongoose validation – the basics
        1. Default validators
          1. All SchemaTypes
          2. Number SchemaType
          3. String SchemaType
      2. Understanding validation errors
      3. Doing it your way – create custom validation
        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
        3. Using multiple re-usable validators
        4. Non-blocking, asynchronous validation
        5. Extending Mongoose validation
      4. Adding validation to our project
      5. Summary
    17. 10. Complex Schemas
      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
      2. Subdocuments
        1. Creating subdocuments
          1. Saving and validation
        2. Retrieving subdocuments
          1. Accessing a specific subdocument
        3. Deleting subdocuments
      3. Data management when modifying existing schemas
      4. Summary
    18. 11. Plugins – Re-using Code
      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
      2. Sharing with the community
      3. Summary
    19. Index

Product information

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