Skip to content

Instantly share code, notes, and snippets.

@rapala61
Last active January 5, 2016 22:11
Show Gist options
  • Save rapala61/bd825654ece1475c41d7 to your computer and use it in GitHub Desktop.
Save rapala61/bd825654ece1475c41d7 to your computer and use it in GitHub Desktop.
Mongoose Model Exercise

Description

We are going to create a mongoose model and use it!

  1. Create a project folder named cardealer and navigate to it.

  2. Run npm init

  3. Run npm install --save mongoose

  4. Using mongoose models, create a model for the following document (car.js):

  • A car
    • should have the following properties:

      • make
      • model
      • color
      • year
      • meta:
        • mpg
        • msrp (manufacturer suggested retail price)
    • The car should have a method that outputs a string that describes the car.

      • example output: "The <:color> <:make> <:model> is a beautiful car. It is eco-friendly. Consumes only <:meta.mpg> mpg!. With a tag price of <:meta.msrp> it's a bargain!"
  1. Using the node CLI node, require mongoose, and connect to the database:
  // Connect to mongo db
  var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/cardealer');
  1. Require your car model:
// or ./models/car depending on where the file is located in the project
var Car = require('./car')
  1. Create 2 different cars and save them to the database.

Deliverable

Slack me the output of the following command:

  var result = Car.find({}, function(err, cars){ console.log(cars) });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment