Coder Social home page Coder Social logo

mongoose-intro's Introduction

General Assembly Logo

An Introduction to Mongoose

The flexibility of MongoDB has a weakness: there's no protection against entering data in any arbitrary format, and no validation of any sort. Mongoose helps with those problems.

Objectives

  • Use Mongoose to help validate data to be stored in MongoDB
  • Use Mongoose as an object-document mapper within a JavaScript program

Instructions

Fork and clone this repository

Mongoose is an Object-Document Mapper

What does that mean? We have objects in our JavaScript and documents in our MongoDB, and Mongoose bridges between them.

We'll learn about Mongoose by building some command line scripts.

Mongoose has Schemas that we use to create Models

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/<database name>');

var Schema = mongoose.Schema;

var contactSchema = new Schema({

  name: {
    first: String,
    last: String
  }

});

var Contact = mongoose.model( 'Contact', contactSchema);

We'll use the models to interact with appropriately named collections of documents. Mongoose maps the model 'Contact' to the collection contacts.

var c = Contact.create({ ...info... });

Virtual attributes

We can add calculated attributes to the model too. These are called 'virtual attributes.' Assume we have name.first and name.last properties: we can derive a name.full property from them.

contactSchema.virtual('name.full').get(function () {
  return this.name.first + ' ' + this.name.last;
});

contactSchema.virtual('name.full').set(function (name) {
  var split = name.split(' ');
  this.name.first = split[0];
  this.name.last = split[1];
});

Using models

We can also use Mongoose query syntax:

var query = Contacts.findOne({ 'name.last': 'Miller' });
query.select('name.first title company');
query.exec(function(err, contact){
    console.log('full name: %s, title: %s, company %s',
        contact.name.full, contact.title, contact.company);
});

Validation

var contactSchema = new Schema({

    // first and last names are required

    name: {
        first: { type: String, required: true },
        last: { type: String, required: true },
    },

    // title is not required

    title: String,

    // age must be between 21 and 65

    age: { type: Number, min: 21, max: 65 },

    // political party must be in the list

    politicalParty: {  type: String,
            enum: { values: 'Republican Democrat Libertarian Green',
                    message: 'enum failed at path {PATH} with value {VALUE}' },
            }
        },

    // phone numbers must match the regular expression

    phoneNumber: { type: String,
        match: /(\d?\D*\d{3}\D*\d{3}\D*\d{4})/ },

});

References

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.