Created
November 18, 2015 18:50
-
-
Save jrobber/38d665380f19836d7499 to your computer and use it in GitHub Desktop.
basic mongoose examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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