2
使用Mongoose時,如何訪問數組中的子文檔?在模式中,我已經按照docs使用對象字面值聲明瞭我的子文檔。Mongoose Object-literal Subdoc Not Accessible
當我檢索到父文檔後,我可以註銷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
那麼,doc.get('children')[0] .name;工程...所以這是預期的行爲還是應該我的原始代碼工作? –
您使用的是mongoose或mongo的舊版本嗎? –
Mongoose 3.8.8和MongoDB 2.4.9 –