var db = mongoose.createConnection(mongodb); var db = mongoose.connect(mongodb);
两种方式对比:
var mongoose = require('mongoose');
db = mongoose.createConnection('localhost', 'test');
var schema = new mongoose.Schema({
name: String
});
var collectionName = 'kittens';
var M = db.model('Kitten', schema, collectionName);
var silence = new M({
name: "Silence"
});
silence.save(function(err){
});
var mongoose = require('mongoose');
db = mongoose.connect('mongodb://localhost/test');
db.once('open', function callback () {
// yay!
});
var kittySchema = mongoose.Schema({ name: String});
var Kitten = mongoose.model('kitten', kittySchema);
var silence = new Kitten({ name: "Silence"});
silence.save(function(err){
});
复制代码
个人博客: http://www.liangtongzhuo.com