반응형
nodejs -- MongoDB 연동
참고 : https://www.npmjs.com/package/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); | |
}); | |
}); |
반응형
'nodejs' 카테고리의 다른 글
nodejs -- socket.io 1.3.7 + jquery 예제 만들기 (수정 예정) (0) | 2015.12.17 |
---|---|
nodejs -- socket.io, express-generator 시작 설정하기 (0) | 2015.11.30 |
nodejs -- mysql 연동 (0) | 2015.11.08 |
nodejs -- WebStorm 에서 nodejs 시작하기 (0) | 2015.10.24 |
nodejs -- OS 별로 terminal 에서 node.js 실행하기 (0) | 2015.10.16 |