我有一个集合,在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} });