Tutorial by Examples

A basic User Schema: var mongoose = require('mongoose'); var userSchema = new mongoose.Schema({ name: String, password: String, age: Number, created: {type: Date, default: Date.now} }); var User = mongoose.model('User', userSchema); Schema Types.
Methods can be set on Schemas to help doing things related to that schema(s), and keeping them well organized. userSchema.methods.normalize = function() { this.name = this.name.toLowerCase(); }; Example usage: erik = new User({ 'name': 'Erik', 'password': 'pass' }); erik.nor...
Schema Statics are methods that can be invoked directly by a Model (unlike Schema Methods, which need to be invoked by an instance of a Mongoose document). You assign a Static to a schema by adding the function to the schema's statics object. One example use case is for constructing custom queries:...
A generic schema useful to work with geo-objects like points, linestrings and polygons. Both Mongoose and MongoDB support Geojson. Example of usage in Node/Express: var mongoose = require('mongoose'); var Schema = mongoose.Schema; // Creates a GeoObject Schema. var myGeo= new Schema({ ...
This kind of schema will be useful if you want to keep trace of your items by insertion time or update time. var mongoose = require('mongoose'); var Schema = mongoose.Schema; // Creates a User Schema. var user = new Schema({ name: { type: String }, ...

Page 1 of 1