mongoose 获取某个存在的collecion 里的数据

在创建Schma 时指定对应的collection

var db_url = 'mongodb://localhost:27017/test';

var db = mongoose.connect(db_url);


db.connection.on('error', function (error) {
console.log('数据库连接失败:' + error);
});
db.connection.on('open', function () {
console.log('——数据库连接成功!——');
});

var Schema = mongoose.Schema;

var userSchema = new Schema({
name : { type:String }
},{ collection: "person"});

var userModel = mongoose.model("usertest", userSchema);

userModel.find({'name':'winyh1'}).select('_id email').exec(function (err, users){
if (!err){
console.log(users);
}
});

posted @ 2017-04-12 16:54  winyh  阅读(1740)  评论(0编辑  收藏  举报