我有一个集合,其中包含 mongo 中的一系列问题。
questions:[
{
"_id" : 1,
"question" : "Some question",
"type" : "type"
}]
我定义了这样一个模式:
var schema = mongoose.Schema({
question: String
, type: String
})
var Model = mongoose.model('question', schema)
查询之后:
new Model.find({}, function(err,records) {
console.log(records[0]._id)
res.render('question/index', {title: 'question', question: records})
})
我在控制台上得到 undefined
或 null
(如果 ID 为 0)。
最佳答案
当您的 _id
值不是 ObjectID 时,您需要在您的模式中定义它。我还建议使用对象定义您的 type
字段以避免潜在问题,因为该关键字在模式定义中具有特殊含义。
var schema = mongoose.Schema({
_id: Number,
question: String,
type: {type: String}
});
关于node.js - Mongoose 不返回自定义 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21511657/