如何通过2条路径上的猫鼬查询填充

时间:2019-01-01 14:23:09

标签: node.js mongodb mongoose

Supplies.find({ clientID })
.populate({
  path: "products",
  model: "products",
  select: "title"
})
.populate("supplier", "name")

我有此查询可返回耗材,我想在产品上填充,收到的第一个文档为true,但是第二个文档不能在产品ID上填充,因为产品ID不在Product._id中,而在Product中。 options._id 选项是一系列文档。

如何在选项数组中以及没有选项数组时填充所有文档 .....

[{
    "_id": "5c2b736fd550e230dc317c7f",
    "product": {
        "_id": "5c23812970bebe20f143c0a2",
        "title": "Iphone 6S"
    },
    "quantity": 2
    "supplier": {
        "_id": "5c0d18447e0f436d5cbb110a",
        "name": "blabla bla"
    }
},
{
    "_id": "5c2b7387d550e230dc317c80",
    "product": "5c23847a217c5521f8b3fd78",
    "quantity": 4,
    "supplier": {
        "_id": "5c0d18447e0f436d5cbb110a",
        "name": "bla bla"
    }
}]

这是产品型号

const ProductSchema = new mongoose.Schema({
       title: { type: String, required: true },
       options: [
      {
      title: { type: String, required: true }
      }]
     });

0 个答案:

没有答案