簡體   English   中英

Mongoose填充子文檔數組

[英]Mongoose populating array of subdocuments

抱歉,如果已經提出這個問題,我的搜索沒有出現同樣的情況。 我有兩個類似下面的模式:

var experimentSchema = new mongoose.Schema({

    name  : 'string'
    elements :  [{
        type : mongoose.Schema.ObjectId, 
        ref: 'Element'
    }],
    resources : [{
        type : mongoose.Schema.ObjectId,
        ref  : 'Resource'
    }],

})


var elementSchema = new mongoose.Schema({
    name : 'string',
    component : {
        type : mongoose.Schema.ObjectId,
        ref  : 'Component'
    }
})

我想要執行一個深度填充,以便當我請求實驗時,我得到一個具有elementsresources數組的對象,並且對於每個元素,還填充了字段component

我嘗試過以下幾點:

Experiment.findOne(query).populate(['resources','elements','elements.component']).exec(...)

沒有成功。 任何人都可以為這種類型的操作提供正確的語法嗎?

提前致謝!

希望這可以幫助。

models.User.findOne(query)
      .populate([{
        path: 'elements',
        populate: { 
             path:  'components', 
             model: 'Component'
            }
      },{
        path:'resources'
      }])
      .exec(...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM