GithubHelp home page GithubHelp logo

mongoose-crud'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, clone, and npm install.

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 a command line script. Think of the script as a similar to a controller and the Mongoose Models as the mechanism to access the data (ActiveRecord).

Mongoose has

We'll use these to create a command line crud app.

Schema

var mongoose = require('mongoose');

var peronsSchema = new mongoose.Schema({

  name: {
    given: String,
    surname: String
  }

});

Model

var Person = mongoose.model( 'Person', PersonSchema);

We'll use the models to interact with appropriately named collections of documents. Mongoose maps the model 'Person' to the MongoDB collection people.

Document

var person = Person.create({...});
// or
var person = new Person({...});
person.save();

Virtual attributes

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

personSchema.virtual('name.full').get(function () {
  return this.name.given + ' ' + this.name.surname;
});

personSchema.virtual('name.full').set(function (name) {
  var split = name.split(' ');
  this.name.given = split[0];
  this.name.surname = split[1];
});

Using models

We'll use Mongoose to query MongoDB:

var query = Person.findOne({ 'name.surname': 'Rollins' });
query.exec().then(function(person) {
    console.log('full name: %s, given: %s, surname %s',
        person.name.full, person.name.given, person.name.surname);
});

Validation

var contactSchema = new Schema({

    // given and surname are required

    name: {
      given: {
        type: String,
        required: true
      },
      surname: {
        type: String,
        required: true
      },
    },

    // title is not required

    title: String
});

References

mongoose-crud's People

Contributors

cwilbur avatar

Watchers

 avatar

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.