Connecting mongoose to database

The process of connecting is going to be pretty similar to what we did inside of our MongoDB scripts; for example, the mongodb-connect script. Here, we called MongoClient.connect, passing in a URL. What we're going to do for Mongoose is call mongoose.connect, passing in the exact same URL; mongodb is the protocol, call in //. We're going to be connecting to our localhost database on port 27017. Next up is going to be our /, followed by the database name, and we'll continue to use the TodoApp database, which we used over in the mongodb-connect script.

var mongoose = require('mongoose');mongoose.connect('mongodb://localhost:27017/TodoApp');

This is where the two functions differ. The MongoClient.connect method ...

Get Advanced Node.js Development now with the O’Reilly learning platform.

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