You are previewing Professional Node.js: Building Javascript Based Scalable Software.

Professional Node.js: Building Javascript Based Scalable Software

  1. Cover
  2. Contents
  3. Part I: Introduction and Setup
    1. Chapter 1: Installing Node
      1. Installing Node on Windows
      2. Installing on Mac OS X
      3. Installing Node Using the Source Code
      4. Setting Up and Using Node Package Manager
      5. Summary
    2. Chapter 2: Introducing Node
      1. Introducing the Event-Driven Programming Style
      2. How Node and JavaScript Make Writing Asynchronous Applications Easier
      3. Summary
  4. Part II: Node Core API Basics
    1. Chapter 3: Loading Modules
      1. Understanding How Node Loads Modules
      2. Exporting a Module
      3. Summary
    2. Chapter 4: Using Buffers to Manipulate, Encode, and Decode Binary Data
      1. Creating a Buffer
      2. Getting and Setting Bytes in a Buffer
      3. Slicing a Buffer
      4. Copying a Buffer
      5. Decoding a Buffer
      6. Summary
    3. Chapter 5: Using the Event Emitter Pattern to Simplify Event Binding
      1. Understanding the Standard Callback Pattern
      2. Understanding the Event Emitter Pattern
      3. Understanding Event Types
      4. Using the Event Emitter API
      5. Creating an Event Emitter
      6. Summary
    4. Chapter 6: Scheduling the Execution of Functions Using Timers
      1. Using setTimeout to Defer the Execution of a Function
      2. Using clearTimeout to Cancel the Execution of a Function
      3. Scheduling and Canceling the Repetitive Execution of a Function
      4. Using process.nextTick to Defer the Execution of a Function Until the Next Event Loop Iteration
      5. Blocking the Event Loop
      6. Escaping the Event Loop
      7. Using setTimeout Instead of setInterval to Force Serialization
      8. Summary
  5. Part III: Files, Processes, Streams, and Networking
    1. Chapter 7: Querying, Reading From, and Writing to Files
      1. Manipulating File Paths
      2. Introducing the fs Module
      3. Opening a File
      4. Reading from a File
      5. Summary
    2. Chapter 8: Creating and Controlling External Processes
      1. Executing External Commands
      2. Spawning Child Processes
      3. Signaling and Killing Processes
      4. Summary
    3. Chapter 9: Reading and Writing Streams of Data
      1. Using a Readable Stream
      2. Using Writable Streams
      3. Considering Some Stream Examples
      4. Avoiding the Slow Client Problem and Saving Your Server
      5. Summary
    4. Chapter 10: Building TCP Servers
      1. Creating a TCP Server
      2. Building a Simple TCP Chat Server
      3. Summary
    5. Chapter 11: Building HTTP Servers
      1. Understanding the http.ServerRequest Object
      2. Understanding the http.ServerResponse Object
      3. Streaming HTTP Chunked Responses
      4. Shutting Down the Server
      5. Example 1: Building a Server that Serves Static Files
      6. Example 2: Making Use of HTTP Chunked Responses and Timers
      7. Summary
    6. Chapter 12: Building a TCP Client
      1. Connecting to a Server
      2. Sending and Receiving Data
      3. Ending the Connection
      4. Handling Errors
      5. Building an Example Command-Line TCP Client
      6. Summary
    7. Chapter 13: Making HTTP Requests
      1. Making GET Requests
      2. Using Other HTTP Verbs
      3. Pooling Sockets Using http.Agent
      4. Using a Third-Party Request Module to Simplify HTTP Requests
      5. Summary
    8. Chapter 14: Using Datagrams (UDP)
      1. Understanding UDP
      2. Understanding the Uses of UDP
      3. Building a Datagram Server
      4. Creating a Simple Datagram Echo Server
      5. Building a Datagram Client
      6. Creating a Simple Datagram Command-Line Client
      7. Understanding and Using Datagram Multicast
      8. Summary
    9. Chapter 15: Securing Your TCP Server With TLS/SSL
      1. Understanding Private and Public Keys
      2. Building a TLS Server
      3. Building a TLS Client
      4. Building Some Examples
      5. Summary
    10. Chapter 16: Securing Your HTTP Server With HTTPS
      1. Building a Secure HTTP Server
      2. Creating an HTTPS Client
      3. Summary
  6. Part IV: Building and Debugging Modules and Applications
    1. Chapter 17: Testing Modules and Applications
      1. Using a Test Runner
      2. Using an Assertion Testing Module
      3. Testing Your Asynchronous Module
      4. Summary
    2. Chapter 18: Debugging Modules and Applications
      1. Using console.log
      2. Using Node’s Built-in Debugger
      3. Using Node Inspector
      4. Summary
    3. Chapter 19: Controlling the Callback Flow
      1. Understanding the Boomerang Effect
      2. Avoiding the Boomerang Effect by Declaring Functions
      3. Using the async Flow Control Library
      4. Summary
  7. Part V: Building Web Applications
    1. Chapter 20: Building and Using HTTP Middleware
      1. Understanding the Connect HTTP Middleware Framework
      2. Building Your Own HTTP Middleware
      3. Using the HTTP Middleware Bundled in Connect
      4. Summary
    2. Chapter 21: Making a Web Application Using Express.js
      1. Initializing Your Express.js Application
      2. Setting Up Middleware in Your Application
      3. Routing Requests
      4. Summary
    3. Chapter 22: Making Universal Real-Time Web Applications Using
      1. Understanding How WebSockets Work
      2. Using Socket.IO to Build WebSocket Applications
      3. Summary
  8. Part VI: Connecting to Databases
    1. Chapter 23: Connecting to MYSQL Using node-mysql
      1. Using a Library to Connect to and Communicate with a MySQL Database
      2. Adding Data to the Database with Security Concerns in Mind
      3. Reading Data Efficiently
      4. Summary
    2. Chapter 24: Connecting to CouchDB Using Nano
      1. Installing Nano
      2. Connecting and Creating a Database
      3. Storing Documents
      4. Creating and Using CouchDB Views
      5. Attaching Files to a CouchDB Document
      6. Summary
    3. Chapter 25: Connecting to MongoDB Using MongooSE
      1. Installing Mongoose
      2. Understanding How Mongoose Uses Models to Encapsulate Database Access
      3. Connecting to MongoDB
      4. Defining a Schema
      5. Defining a Model
      6. Summary
  9. Introduction
  10. Advertisements

Chapter 25

Connecting to MongoDB Using Mongoose


  • Installing and using Mongoose
  • Defining schemas and models
  • Understanding Mongoose data types
  • Using schema validation
  • Searching, inserting, and updating documents
  • Paginating query results
  • Defining indexes
  • Using getters, setters, and virtual attributes
  • Referencing documents
  • Enriching models and document objects with higher-level methods

Document-oriented databases have regained popularity in recent years, being at the front and center of the NoSQL movement. These types of databases, unlike relational ones, don’t require you to specify the structure of your data ahead of time. Instead, you can change it organically as your project requires. Another important difference from a relational database is that the document is generally the atomic unit and can be as complete as needed, eliminating the need to make costly joins and transactions across tables.

MongoDB is a popular document-oriented database that enables you to perform rich dynamic queries using a powerful query language.

In MongoDB, data is organized such that one MongoDB database holds a series of collections. Each collection has a series of documents, and each document comprises a set of fields. A field is a key-value pair, where the key is a string and the value can be a basic type (like a string, integer, float, timestamp, binary, and so on), an embedded document, or a collection of any of these.

A MongoDB query can be described using a JavaScript ...

The best content for your career. Discover unlimited learning on demand for around $1/day.