簡體   English   中英

問題填充子 collections 使用 MongoDB 和 Mongoose

[英]Issue populating child collections using MongoDB and Mongoose

我有以下架構:

const MenuSchema = new mongoose.Schema({
  name: String,
  type: String,
  children: [{ type: ObjectId, ref: 'Menu' }],
});

以及以下查詢:

    const item = await Menu.findOne({ _id: <id> }).populate({
        path: 'children',
    });
    console.log(item.children);

我試圖用實際的相關文檔遞歸地填充子數組,我可以填充第一個集合,但我需要有效地遞歸地執行此操作。

我想你可以做

 const item = await Menu.findOne({ _id: <id> }).populate('children');
 console.log(item.children);

.populate 將路徑作為第一個參數,無需輸入 object

暫無
暫無

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

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