Skip to content

Instantly share code, notes, and snippets.

@jrobber
Created November 18, 2015 18:50
Show Gist options
  • Save jrobber/38d665380f19836d7499 to your computer and use it in GitHub Desktop.
Save jrobber/38d665380f19836d7499 to your computer and use it in GitHub Desktop.
basic mongoose examples
var mongoose = require('mongoose');
var birdModel = mongoose.Schema({
scientificName: {type: String, required:true, unique:true, lowercase:true},
color: {type: String, required:true, unique:true},
region: String,
firstSightingEver: Date,
food: [String],
foodDetails: [{
name: String,
type: {type: String},
genus: String
}],
wingspan: Number,
endangered: Boolean,
nest: {
materials: [String],
size: Number,
timeToBuild: Number,
locationDesc: String
}
})
birdModel.pre('save', function(next){
var bird = this;
bird.scientificName.toLower();
next();
})
module.exports = mongoose.model('bird', birdModel);
/*
var howIFeel = {
headache: true,
frustrationLevel: 100,
}
module.exports = howIFeel
*/
var Bird = require('./birdsModel')
Bird.find({}, function(err, response){
})
Bird.find()
.where('color').equals('Blood red')
.where('wingspan').gt(12)
.sort(-'wingspan')
.limit(10)
.exec(function(err, response){
})
Bird.find()
.where('color').equals('Blood red')
.where('wingspan').gt(12)
.sort(-'wingspan')
.limit(10)
.exec()
.then(function(results){
//handle results
}, function(err){
//Handle err
})
var newBird = new Bird({
scientificName: "Genus Menus Birdus Maximus",
color: "Blood Red",
region: "Sparta",
//etc
});
newBird.save(function(err, result){
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment