2015-06-16 93 views
0

我想从mongo中找到一个文档,但是findOne()将带有一个未定义的_id字段的文档。 为什么?猫鼬不检索_id

var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost/school'); 
var Schema = mongoose.Schema; 

var scoreSchema = new Schema({ 
    type: String, 
    score: Number 
}); 

var studentSchema = new Schema({ 
    name: String, 
    scores: [scoreSchema] 
}); 

var mod = mongoose.model('Student', studentSchema); 
mod.findOne(function(err, stud) { 
     console.log('id:' + stud._id); 
}); 

回答

2

您需要传递一些信息以在查询中找到。例如:

mod.findOne({name: 'John'}, function(err, stud) { 
    console.log('id:' + stud._id); 
}); 

请参阅here关于如何在Mongoose中进行查询。

0

您不查询任何内容,请在回调前注意{}

mod.findOne({}, function(err, stud) { 
    console.log('id:' + stud._id); 
}); 

您可能想看看Mongoose documentation