如果我有单独的映射集合,如何使用 populate 方法?

问题描述 投票:0回答:1

我有以下收藏


const itemSchema = new mongoose.Schema({
  name: String,
  // other item properties
});

const storeSchema = new mongoose.Schema({
  name: String,
  // other store properties
});

const storeItemSchema = new mongoose.Schema({
  store: { type: mongoose.Schema.Types.ObjectId, ref: 'Store' },
  item: { type: mongoose.Schema.Types.ObjectId, ref: 'Item' },
  // other storeItem properties
});

我正在尝试获取包含如下商品的商店,但出现错误

Cannot populate path "items" because it is not in your schema. Set the "strictPopulate" option to false to override

return this.storeModel.find({},{_id:1}).populate('items')
mongodb mongoose nestjs
1个回答
0
投票

无法填充路径“items”,因为它不在您的架构中。将“strictPopulate”选项设置为 false 以覆盖

请检查以下声明:populate 中给出的路径是“items”,根据架构 - storeItemSchema,它必须是“item”。

return this.storeModel.find({},{_id:1}).populate('items')
© www.soinside.com 2019 - 2024. All rights reserved.