Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mongoose for Application Development

You're reading from   Mongoose for Application Development 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.

Arrow left icon
Product type Paperback
Published in Aug 2013
Publisher Packt
ISBN-13 9781782168195
Length 142 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Simon Holmes Simon Holmes
Author Profile Icon Simon Holmes
Simon Holmes
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Mongoose for Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Introducing Mongoose to the Technology Stack 2. Establishing a Database Connection FREE CHAPTER 3. Schemas and Models 4. Interacting with Data – an Introduction 5. Interacting with Data – Creation 6. Interacting with Data – Reading, Querying, and Finding 7. Interacting with Data – Updating 8. Interacting with Data – Deleting 9. Validating Data 10. Complex Schemas 11. Plugins – Re-using Code Index

Connecting our project


Now we know what we're doing, let's connect our project to a database using the default Mongoose connection.

Creating the connection

For the sake of well organized code, let's create a folder called model, and in that, an empty JavaScript file called db.js. We'll use this for managing the Mongoose connection, and will add to it in later chapters.

At this stage the file needs to do three things:

  1. Bring in the Mongoose module

  2. Build the connection string for the database

  3. Open the Mongoose connection to the database

So in your /model/db.js file, enter the following:

// Bring Mongoose into the project
var mongoose = require( 'mongoose' );

// Build the connection string
var dbURI = 'mongodb://localhost/MongoosePM';

// Create the database connection
mongoose.connect(dbURI);

Each of the three objectives is achieved with just one line of code—pretty simple don't you think!

Catching the events

Next up we want to set up our event handlers. At this stage, we are just going to log messages...

You have been reading a chapter from
Mongoose for Application Development
Published in: Aug 2013
Publisher: Packt
ISBN-13: 9781782168195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime