簡體   English   中英

Mongoose 使用“路徑”選項填充在子文檔數組中不起作用

[英]Mongoose Populate not working in array of sub-document with “path” option

這是我的 Mongoose 架構:

const productSchema = mongoose.Schema({
  writer: {
        type: Schema.Types.ObjectId,
        ref: 'User'
  },
  schedule: [{
            dateTime: {
                type: String,
                unique: 1            
            },
            candidates: [{
                type: Schema.Types.ObjectId,
                ref: 'User'
            }],
            attorn: {
                type: Schema.Types.ObjectId,
                ref: 'User'
            }
        }]
   ...

這是我與候選人一起獲得物品的地方:

    Product.find({ '_id': { $in: productIds } })
    .populate('writer')
    .populate({ 
        path: 'schedule',
        populate: {
          path: 'candidates',
          model: 'User'
        } 
     })
    .exec((err, product) => {
        if (err) return res.status(400).send(err)
        return res.status(200).send(product)
    })
  1. 此代碼的結果是一個產品,其中僅填充了“writer”和“candidates”的空數組
  2. 如果我刪除填充“候選人”的行,它將返回“候選人”的_id數組
  3. 如何填充“候選人”數組?

你能試試這個嗎

Product.find({ _id: { $in: productIds } })
  .populate("writer")
  .populate({
    path: "schedule.candidates",
  })
  .populate({
    path: "schedule.attorn",
  })
  .exec((err, product) => {
    if (err) return res.status(400).send(err);
    return res.status(200).send(product);
  });

暫無
暫無

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

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