Mongoose for Application Development


Mongoose for Application Development
eBook: $23.99
Formats: PDF, PacktLib, ePub and Mobi formats
$20.39
save 15%!
Print + free eBook + free PacktLib access to the book: $63.98    Print cover: $39.99
$39.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Support
Sample Chapters
  • 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

Book Details

Language : English
Paperback : 142 pages [ 235mm x 191mm ]
Release Date : August 2013
ISBN : 1782168192
ISBN 13 : 9781782168195
Author(s) : Simon Holmes
Topics and Technologies : All Books, Application Development, Open Source

Table of Contents

Preface
Chapter 1: Introducing Mongoose to the Technology Stack
Chapter 2: Establishing a Database Connection
Chapter 3: Schemas and Models
Chapter 4: Interacting with Data – an Introduction
Chapter 5: Interacting with Data – Creation
Chapter 6: Interacting with Data – Reading, Querying, and Finding
Chapter 7: Interacting with Data – Updating
Chapter 8: Interacting with Data – Deleting
Chapter 9: Validating Data
Chapter 10: Complex Schemas
Chapter 11: Plugins – Re-using Code
Index
  • Chapter 1: Introducing Mongoose to the Technology Stack
    • The technology stack – Node.js, npm, MongoDB, and Express
      • The language and the server – JavaScript and Node
      • Single-threaded versus multithreaded
        • Blocking versus non-blocking code
      • The database – MongoDB
      • The framework – Express
    • What Mongoose is all about
      • What is Mongoose good for?
      • What Mongoose is not ideally suited for
      • The cornerstones of Mongoose
        • Mongoose schemas
        • Mongoose models
    • Installing the full stack
      • Installing the prerequisites
      • Installing Node.js
      • Installing npm
      • Installing MongoDB
      • Installing Express.js
      • Installing Mongoose
        • Direct installation into project
        • Using project dependencies – package.json
      • Creating a project
    • Summary
    • Chapter 2: Establishing a Database Connection
      • Mongoose default connection
        • Using multiple connections
      • About the connection string
        • Setting the port
        • Specifying a database user
      • Connection options
      • Closing the connection
        • Calling the close command
        • Closing when the Node process ends
      • Connection events
      • Connecting our project
        • Creating the connection
        • Catching the events
        • Opening the connection at application start
        • Creating the database
      • Summary
      • Chapter 3: Schemas and Models
        • Introducing schemas
          • Field sizes
        • Data types allowed in schemas
          • String
          • Number
          • Date
          • Boolean
          • Buffer
          • ObjectId
          • Mixed
            • Tracking changes to Mixed type
          • Array
            • Warning – array defined as mixed type
          • Custom SchemaTypes
        • Where to write the schemas
        • Writing a schema
          • Modifying an existing schema
          • Setting a default value
          • Only allowing unique entries
          • Our final User schema
          • What's that "__v" thing?
            • Why is this needed?
          • Defining the Project schema
            • Improving the Project schema
        • Building models
          • Instances
            • Interacting with instances
            • Finding a single instance
            • Finding many instances
          • Considerations when choosing your model name
        • Setting the collection name
          • Overriding the collection name in the schema
          • Overriding the collection name in the model
          • Building models from our schemas
        • Our complete code
        • Summary
        • Chapter 4: Interacting with Data – an Introduction
          • Model methods and instance methods
          • Setting up the project
            • Code structure
              • Adding the routes files
              • Tying the routes to Mongoose models
            • URLs and routes
              • Routes for user management
              • Routes for project management
          • Summary
          • Chapter 5: Interacting with Data – Creation
            • Creating an instance
              • Adding data to the instance
            • Saving an instance
              • Using the saved data
            • Creating and saving database entry in one step
              • Chaining methods
              • The Model.create() method
            • CRUD – create data
              • Adding a new user form
                • Adding the Jade template
                • Linking the view to the URL
              • Adding the create user function
                • Error trapping
                • Creating a user session
              • Displaying the confirmation page
              • Try it out!
              • Adding create project functionality
                • Routes
                • New files and functions
            • Summary
            • Chapter 6: Interacting with Data – Reading, Querying, and Finding
              • Approaches to find and read data
                • Using the QueryBuilder
                • Single query operation
                • Static helper methods – finding data
              • CRUD – reading user and project data
                • findOne() – finding a single user
                  • Login form
                  • Login action
                  • Housekeeping – adding homepage links
                  • Try it out!
                • find() – finding a list of projects and returning JSON to AJAX
                  • Creating a new static find method
                  • Setting up the route
                  • Updating the view
                  • Building the AJAX call
                  • Try it out!
                • findByld() – finding a single project
                  • Route setup
                  • Creating the view
              • Summary
              • Chapter 7: Interacting with Data – Updating
                • Model helper methods
                  • Building the commands
                  • Which method to choose
                  • The catch
                • The three-step find-edit-save approach
                • CRUD – editing users and projects
                  • Tracking user login
                  • Editing the current user
                    • Routes in use
                    • Setting up the form
                    • Setting up the controllers
                    • Committing the edit
                  • Editing projects
                    • Routes
                    • New files and functions
                • Summary
                  • Chapter 9: Validating Data
                    • Mongoose validation – the basics
                      • Default validators
                        • All SchemaTypes
                        • Number SchemaType
                        • String SchemaType
                    • Understanding validation errors
                    • Doing it your way – create custom validation
                      • Single function – no custom error message
                      • Returning a custom error message
                        • Validating a regular expression
                        • Taking the messages out of the schema
                      • Using multiple re-usable validators
                      • Non-blocking, asynchronous validation
                      • Extending Mongoose validation
                    • Adding validation to our project
                    • Summary
                    • Chapter 10: Complex Schemas
                      • Population – references to other collections
                        • Defining data for population
                        • Saving population references
                        • Retrieving populated data
                          • Querying to return a subset of results
                          • Populating into multiple parent items
                      • Subdocuments
                        • Creating subdocuments
                          • Saving and validation
                        • Retrieving subdocuments
                          • Accessing a specific subdocument
                        • Deleting subdocuments
                      • Data management when modifying existing schemas
                      • Summary
                      • Chapter 11: Plugins – Re-using Code
                        • Reusable schema plugins
                          • Creating a schema plugin
                            • Applying the plugin to an existing schema
                            • Using an external file
                            • Using schema middleware
                        • Sharing with the community
                        • Summary

                        Simon Holmes

                        Simon Holmes started his journey as a web developer in the late 1990s. He built his first website for a project at university and soon saw what the industry had to offer when he promptly sold it! Following university, Simon worked his way through the ranks of design agency life, learning the skills of becoming a full-stack web developer. From server management and database design to building dynamic UIs from Photoshop files, it all fell under Simon’s remit. Having witnessed first-hand the terrible JavaScript code so prevalent in the early 2000s, Simon is very much enjoying its resurgence as a powerful, structured language. Simon now works in SaaS, which is very heavy on the JavaScript.

                        Sorry, we don't have any reviews for this title yet.

                        Code Downloads

                        Download the code and support files for this book.


                        Submit Errata

                        Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                        Errata

                        - 2 submitted: last submission 27 Feb 2014

                        Errata type: Typo | page number: 56 | Errata date: September 11, 2013

                        this sentence:

                        We don't always need to run all the commands separately  and can simplify do
                        things by creating and saving the database entry in one step.

                        should be:

                        We don't always need to run all the commands separately  and can simply do
                        things by creating and saving the database entry in one step.

                        Errata type: code | errata page: 76

                        The code snippet at the end of the page:

                        block headjs
                        script()

                        var userID = "#{userID}";
                        script(src="/javascripts/user.js")

                        Should be:

                        block headjs
                        script.
                        var userID = "#{userID}";
                        script(src="/javascripts/user.js")

                        Sample chapters

                        You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                        Frequently bought together

                        Mongoose for Application Development +    Express Web Application Development =
                        50% Off
                        the second eBook
                        Price for both: €29.90

                        Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                        What you will learn from this book

                        • Learn the different methods of using Mongoose to connect to a database
                        • Learn to define schemas and building data models
                        • Interact with data using schema methods
                        • Create databases, collections, and data
                        • Query the database to find specific data
                        • Edit, save, and delete data
                        • Validate data using Mongoose validators
                        • Use population to link datasets
                        • Use sub-documents and nested schemas to provide data depth
                        • Get to grips with code efficiency through re-usable plugins

                        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.

                        Approach

                        This book is a mini tutorial full of code examples and strategies to give you plenty of options when building your own applications with MongoDB.

                        Who this book is for

                        This book is ideal for people who want to develop applications on the Node.js stack quickly and efficiently. Prior knowledge of the stack is not essential as the book briefly covers the installation of the core components and builds all aspects of the example application. The focus of the book is on what Mongoose adds to you applications, so experienced Node.js developers will also benefit.

                        Code Download and Errata
                        Packt Anytime, Anywhere
                        Register Books
                        Print Upgrades
                        eBook Downloads
                        Video Support
                        Contact Us
                        Awards Voting Nominations Previous Winners
                        Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                        Resources
                        Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software