1. Intro
  2. Setup
  3. The Schema
  4. Models
  5. Methods
  6. Example Data
  7. Writing to DB
  8. Pulling Data


Mongoose is a great javascript framework that makes working in Node with Mongoo a breeze!  It’s basicallt an ODM (Object Data Mapper), and gives you an easy wat to define a schema with some helper functions as well.


So since we are working with Node and Mongo, you are going to want to make sure both are installed and working.

We aren’t really going to use Express or anything, we want to keep this example simple.  Following the docs on the Mongoose site lets get the following going

//requiring mongoose in app
const mongoose = require("mongoose");

//connecting Mongo database
mongoose.connect('mongodb://localhost/tacos', {
    useNewUrlParser: true,
    useUnifiedTopology: true

const db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    console.log("MongoDB Connected");

So we’ve first off calling in the Mongoose package (make sure you npm installed it!)

Then using that mongoose objects connect method, we connected to the mongo instance using the tacos collection.

You should now a console.log of “MongoDB Connected” if everything was configured correctly.


So yes, I know Mongo is non-relational, but we still want to have some type of guide as to what the heck goes into our collection of documents!  We won’t be locked into it, but it will help prevent bugs.aa

const mongoose = require('mongoose');
const Schema   = mongoose.Schema;
const tacoShop = new Schema({
    name:  String,
    owner: String,
    tagline: String,
    menu: [{ itemName: String, date: Date }],
    open: Boolean

Okay, so now that we have a schema to follow we have to create a model.  While a schema like the one above allowed Mongoose to validate info going in, we have to wire up a model.  The methods for working with the data, are on the model.

const TacoShop = mongoose.model(‘tacoShop’, tacoShop);

One thing you want to keepm in mind is that all methods must come before Model completion.