如何使用mongodb中的Id将详细信息从一个集合获取到另一个集合

时间:2017-05-25 10:16:01

标签: node.js mongodb mongoose

我有一个ordercollection,因为我在productDetails中存储了productDetails(它可能包含对象数组),我正在使用" products"对象(这也可能是一个对象数组,取决于订购的产品数量),所以通过使用内部"产品"如何获得特定的产品细节。以下是我的订单集

{
"_id": ObjectId("59267748c1f80b17cd53a7bd"),
"updatedAt": ISODate("2017-05-25T06:33:41.827Z"),
"createdAt": ISODate("2017-05-25T06:18:48.264Z"),
"createdBy": "58ba6b8356a7bf05f613cda0",
"status": "shipment",
"productDetails": [
    {
        "_id": ObjectId("59267997c1f80b17cd53a7da"),
        "productCatalogId": "592677eac1f80b17cd53a7be",
        "quantity": 2,
        "products": [
            {
                "productId": "5926791bc1f80b17cd53a7d1",
                "_id": ObjectId("59267a94c1f80b17cd53a7ee")
            },
            {
                "productId": "5926793bc1f80b17cd53a7d3",
                "_id": ObjectId("59267aacc1f80b17cd53a7ef")
            }
        ]
    },
    {
        "_id": ObjectId("592679a1c1f80b17cd53a7db"),
        "productCatalogId": "592678a5c1f80b17cd53a7c5",
        "quantity": 1,
        "products": [
            {
                "productId": "59267959c1f80b17cd53a7d5",
                "_id": ObjectId("59267ac5c1f80b17cd53a7f0")
            }
        ]
    }
],
"__v": 0,
"OrderedFor": "58ba6b8356a7bf05f613cda0",
"billingId": ObjectId("592679d2c1f80b17cd53a7e2"),
"shipmentId": ObjectId("592679d2c1f80b17cd53a7e3")

}

这是我的产品系列

{
"_id": ObjectId("5926791bc1f80b17cd53a7d0"),
"updatedAt": ISODate("2017-05-25T06:32:52.590Z"),
"createdAt": ISODate("2017-05-25T06:26:35.529Z"),
"productCatalogId": "592677eac1f80b17cd53a7be",
"batch": "2017-05-25T06:26:35.510Z",
"createdBy": "58be7c914e5db93569289d27",
"products": [
    {
        "updatedAt": ISODate("2017-05-25T06:26:35.528Z"),
        "createdAt": ISODate("2017-05-25T06:26:35.528Z"),
        "serialno": "1016SAM0001",
        "mfgdate": ISODate("2017-04-21T18:30:00.000Z"),
        "mfgbatch": "1016",
        "updatedBy": "58be7c914e5db93569289d27",
        "_id": ObjectId("5926791bc1f80b17cd53a7d1"),
        "status": "sold",
        "addOnSerialnos": [
            "1016RAK0001",
            "1016SUJ0001"
        ]
    }
],
"__v": 0

}

我的查询是

exports.getInvoiceDetails = function(req, res) {
Order.findById(req.params.id)
    .populate('shipmentId', 'shipmentId deliveryAddress deliveryContactPerson deliveryContact isCashonDelivery')
    .populate('productDetails.productCatalogId', 'productDetails.productCatalogId language price.mrp warantydays name model version')
    .populate('billingId', 'billingId billingAddress paymentMode grandTotalAmountPaid paymentStatus invoiceNumber invoiceItems')
    .populate('productDetails.products.productId', 'serialno addOnSerianos')
    .exec(function(error, order) {
        if (error) return res.json(error);
        return res.json(order);
    });

};

0 个答案:

没有答案