【发布时间】:2014-05-21 09:31:57
【问题描述】:
使用 Mongoose,我如何访问数组中的子文档?在架构中,我根据docs 使用对象文字声明了我的子文档。
在我检索到父文档后,我可以注销 doc.children 并查看对象数组,但是当我尝试访问任何文档时,我无法定义。 doc.children 没有作为数组返回,那么如何访问子文档呢?
架构:
var parentSchema = new Schema({
children: [{ name: 'string' }]
});
用法:
console.log(doc.children); //[{name: 'hello'}, {name: 'world'}]
doc.children[0]; //undefined
doc.children['0']; //undefined
【问题讨论】:
标签: node.js mongodb mongoose subdocument