Pio*_*Pio 2 mongoose mongodb node.js
我有一个集合,在mongo中包含一系列问题.
questions:[
{
"_id" : 1,
"question" : "Some question",
"type" : "type"
}]
Run Code Online (Sandbox Code Playgroud)
我已经定义了这样的架构:
var schema = mongoose.Schema({
question: String
, type: String
})
var Model = mongoose.model('question', schema)
Run Code Online (Sandbox Code Playgroud)
并在查询之后:
new Model.find({}, function(err,records) {
console.log(records[0]._id)
res.render('question/index', {title: 'question', question: records})
})
Run Code Online (Sandbox Code Playgroud)
我undefined上了控制台或null(如果ID为0).
当您的_id值不是ObjectID时,您需要在架构中定义它.我还建议type使用对象定义您的字段以避免潜在的问题,因为该关键字在模式定义中具有特殊含义.
var schema = mongoose.Schema({
_id: Number,
question: String,
type: {type: String}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
568 次 |
| 最近记录: |