Created
November 7, 2015 16:10
nodejs ; MongoDB 연동 --- mongoose 사용, 먼저 몽고디비 외부접속 허용해야한다.
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'); | |
mongoose.connect('mongodb://192.168.13.129/test'); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', function callback() { | |
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 fluffy = new Kitten({ | |
name : 'fluffy' | |
}); | |
fluffy.speak(); | |
fluffy.save(function(err, fluffy) { | |
if (err) { | |
// TODO handle the error | |
throw err; | |
} | |
}); | |
Kitten.find(function(err, kittens) { | |
if (err) { | |
// TODO handle err | |
throw err; | |
} | |
db.close(); | |
console.log('Kittens : ' + kittens); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment