如何在示例文档中填充产品数组的“ OEM,类别,子类别”:

时间:2019-05-21 13:14:55

标签: node.js mongoose

这是我的路由器JS,可通过Mongoose获取文档。所以问题是我有这些嵌套数据,如oem,category和subcategory,这些我无法获取全部细节。自一个星期以来,我一直在尝试,但没有一个符合我的要求。有人可以帮我吗?

router.get("/product/:id/:product_id", (req, res) => {


 ProReg.findById(req.params.id)
    .populate({
      path: "products",
      populate: [
        {
          path: "products.oem",
          model: "company",
          select: "companyname"
        },
        {
          path: "category",
          model: "productcategory",
          select: "category"
        },
        {
          path: "subcategory",
          model: "productsubcategory",
          select: "subcategory"
        }
      ]
    })
    .exec(function(err, products) {
      if (err) {
        res.json(err);
      } else {
        res.json(products);
      }
    });
});

这是我得到的结果

{
"_id": "5cd37f4fcd79b01040124dbc",
"refno1": "klklklkl",
"refno2": "klklklkl",
"date": "2019-05-08T00:00:00.000Z",
"customer": "5c98bb0a42207b16d8fbd3cf",
"customertype": "5c7a1a1d4913fa08ac75c027",
"department": "5cbd67c709aeca1ea480157a",
"products": [],
"__v": 3
}

不仅提供产品详细信息。现在如何获取该产品数据以及OEM,类别和子类别的详细信息?

没有填充,这是结果:

  {
    "_id": "5cd37f4fcd79b01040124dbc",
    "refno1": "klklklkl",
    "refno2": "klklklkl",
    "date": "2019-05-08T00:00:00.000Z",
    "customer": {
        "_id": "5c98bb0a42207b16d8fbd3cf",
        "customername": "Raghav"
    },
    "customertype": {
        "_id": "5c7a1a1d4913fa08ac75c027",
        "customertype": "Government "
    },
    "department": {
        "_id": "5cbd67c709aeca1ea480157a",
        "department": "Hardware"
    },
    "products": [
        {
            "warrantyfrom": "2019-05-09T00:00:00.000Z",
            "warrantyto": "2019-05-30T00:00:00.000Z",
            "oemwarrantyfrom": "2019-05-14T00:00:00.000Z",
            "oemwarrantyto": "2019-05-14T00:00:00.000Z",
            "_id": "5cd37f60cd79b01040124dbd",
            "oem": "5cb6e026042460131454cf45",
            "category": "5c960902e5cf3429e06beb6c",
            "subcategory": "5ca35fbed6e1430df88954a6",
            "modelno": "A123888",
            "serialno": "fdsfdsfs"
        },
        {
            "warrantyfrom": "2019-05-09T00:00:00.000Z",
            "warrantyto": "2019-05-30T00:00:00.000Z",
            "oemwarrantyfrom": "2019-05-14T00:00:00.000Z",
            "oemwarrantyto": "2019-05-14T00:00:00.000Z",
            "_id": "5cd37f65cd79b01040124dbe",
            "oem": "5cb6e026042460131454cf45",
            "category": "5c960902e5cf3429e06beb6c",
            "subcategory": "5ca35fbed6e1430df88954a6",
            "modelno": "A123888",
            "serialno": "eeeeee"
        },
        {
            "warrantyfrom": "2019-05-09T00:00:00.000Z",
            "warrantyto": "2019-05-30T00:00:00.000Z",
            "oemwarrantyfrom": "2019-05-14T00:00:00.000Z",
            "oemwarrantyto": "2019-05-14T00:00:00.000Z",
            "_id": "5cd37f6fcd79b01040124dbf",
            "oem": "5c986a1e9b6bc614b8a551b9",
            "category": "5c960902e5cf3429e06beb6c",
            "subcategory": "5ca35fbed6e1430df88954a6",
            "modelno": "QW123",
            "serialno": "hhhhhh"
        }
    ],

1 个答案:

答案 0 :(得分:0)

您只是尝试这样做吗?

ProReg.findById(req.params.id)
    .populate('products.oem')
    .populate('products.category')
    .populate('products.subcategory')
    .exec(function(err, products) {
      if (err) {
        res.json(err);
      } else {
        res.json(products);
      }
    });
});

查看这些相关的票证:

Mongoose populate within an object?

Mongoose populate with array of objects containing ref