Created
March 10, 2014 17:00
-
-
Save MichaelSEA/9469105 to your computer and use it in GitHub Desktop.
simple quickstart for mongoose.js from http://mongoosejs.com/docs/index.html
This file contains hidden or 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
// getting-started.js from http://mongoosejs.com/docs/index.html | |
var mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/test'); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, "connection error:")); | |
db.once('open', function callback () { | |
// yay! | |
var kittySchema = mongoose.Schema({ name: String }); | |
kittySchema.methods.speak = function () { | |
var greeting = this.name | |
? "Meow name is " + this.name | |
: "I don't have a name"; | |
console.log(greeting); | |
} | |
var Kitten = mongoose.model('Kitten', kittySchema); | |
var silence = new Kitten({ name: 'Silence' }) | |
console.log(silence.name); // 'Silence' | |
var fluffy = new Kitten({ name: 'fluffy' }); | |
fluffy.speak(); // "Meow name is fluffy" | |
fluffy.save(function (err, fluffy) { | |
if (err) return console.error(err); | |
fluffy.speak(); | |
}); | |
Kitten.find(function (err, kittens) { | |
if (err) return console.error(err); | |
console.log(kittens) | |
}); | |
Kitten.find({ name: /^fluff/ }, simpleCallback); | |
}); | |
function simpleCallback (err, results) { | |
console.log(results); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment