MongoDB-对从$ lookup收到的数据使用过滤器

时间:2018-12-01 09:46:06

标签: mongodb mongodb-query aggregation-framework

我试图过滤从查找操作接收到的mongodb数据上的数据,但我无法弄清楚,任何帮助将不胜感激(也希望我能正确地解释问题,让我知道是否我可以对此进行改进)

让我先共享输出:

    {
        "success": true,
        "message": [
            {
                "paymentId": "",
                "cartId": [
                    "5bd072dca17e9a410326cbcc"
                ],
                "itemId": [
                    {
                        "paymentId": "",
                        "cartId": [
                            "5bd072dca17e9a410326cbcc"
                        ],
                        "status": "init",
                        "cod": false,
                        "_id": "5bd07333a17e9a410326cbcd",
                        "userId": "5b711d6572b22b0ecc361345",
                        "productId": "5ba5f95d0ba5cc3a8d1d98ae",
                        "productDetails": {
                            "_id": "5ba5f95d0ba5cc3a8d1d98ae",
                            "stock": 5000,
                            "gst": 12,
                            "images": [
                                "http://houzzcart.com/uploads/1538155710187.jpg",
                                "http://houzzcart.com/uploads/1538155710184.jpg",
                                "http://houzzcart.com/uploads/1538155710186.jpg",
                                "http://houzzcart.com/uploads/1538155710185.jpg"
                            ],
                            "visible": true,
                            "title": "Vertical Garden By Houzzcart",
                            "subTitle": "Green Walls",
                            "basePrice": 999,
                            "measurementUnit": "Pc",
                            "weight": 0.5,
                            "slug": "vertical-garden-by-houzzcart-7473",
                            "sellerId": {
                                "_id": "5b5c747d8209982630bbffe5",
                                "pin": "110008",
                                "companyName": "Retail ",
                                "firstName": "Houzzcart ",
                                "lastName": "Singh "
                            },
                            "commission": 10,
                            "offerId": {
                                "discount": 52,
                                "title": "Discount 52%"
                            },
                            "sellingPrice": 699,
                            "discountedPrice": "336",
                            "discount": 52,
                            "finalPrice": 376,
                            "gstTax": "40",
                            "deliveryCharges": 0
                        },
                        "quantity": 1,
                        "gst": 12,
                        "discount": 52,
                        "commission": 10,
                        "__v": 0,
                        "createdAt": "2018-10-24T13:27:15.446Z",
                        "updatedAt": "2018-10-24T13:27:15.446Z"
                    },
{
                    "_id": "5bceefc85bba4826d22f7435",
                    "paymentId": "",
                    "cartId": [
                        "5bb71466b7c4842a42e43541"
                    ],
                    "status": "init",
                    "cod": false,
                    "userId": "5b711d6572b22b0ecc361345",
                    "productId": "5b9f899a175f473fd9c15c86",
                    "productDetails": {
                        "_id": "5b9f899a175f473fd9c15c86",
                        "stock": 100,
                        "gst": 18,
                        "images": [
                            "http://houzzcart.com/uploads/1537182178833.jpg",
                            "http://houzzcart.com/uploads/1537182178834.jpg"
                        ],
                        "visible": true,
                        "subTitle": "Corsa Series",
                        "title": "Centre Hole Basin Mixer Corsa",
                        "weight": 1,
                        "basePrice": 100,
                        "measurementUnit": "Pc",
                        "sellerId": {
                            "_id": "5b5c747d8209982630bbffe5",
                            "pin": "110008",
                            "companyName": "Retail ",
                            "firstName": "Houzzcart ",
                            "lastName": "Singh "
                        },
                        "slug": "centre-hole-basin-mixer-corsa-1",
                        "commission": 10,
                        "offerId": {
                            "discount": 51,
                            "title": "Discount 51%"
                        },
                        "sellingPrice": 499,
                        "discountedPrice": "245",
                        "discount": 51,
                        "finalPrice": 289,
                        "gstTax": "44"
                    },
                    "quantity": 1,
                    "gst": 18,
                    "discount": 51,
                    "commission": 10,
                    "__v": 0,
                    "createdAt": "2018-10-23T09:54:16.534Z",
                    "updatedAt": "2018-10-23T09:54:16.534Z"
                },
                {
                    "_id": "5bceefc85bba4826d22f7436",
                    "paymentId": "",
                    "cartId": [
                        "5bc097ede250fe310785c328"
                    ],
                    "status": "init",
                    "cod": false,
                    "userId": "5b711d6572b22b0ecc361345",
                    "productId": "5ba527a10ba5cc3a8d1d9884",
                    "productDetails": {
                        "_id": "5ba527a10ba5cc3a8d1d9884",
                        "stock": 5000,
                        "gst": 18,
                        "images": [
                            "http://houzzcart.com/uploads/1537551879671._SL1500_",
                            "http://houzzcart.com/uploads/1537551879672._SL1500_",
                            "http://houzzcart.com/uploads/1537552273321._SL1500_",
                            "http://houzzcart.com/uploads/1537552273323._SL1500_",
                            "http://houzzcart.com/uploads/1537552273324._SL1500_",
                            "http://houzzcart.com/uploads/1537552273325._SL1500_",
                            "http://houzzcart.com/uploads/1537552273320.PNG",
                            "http://houzzcart.com/uploads/1537612831788._SL1001_",
                            "http://houzzcart.com/uploads/1537612831787._SL1001_",
                            "http://houzzcart.com/uploads/1537612831786._SL1080_"
                        ],
                        "visible": true,
                        "title": "Hanging Flower Wisteria By Houzzcart ",
                        "basePrice": 90,
                        "weight": 0.5,
                        "subTitle": "Vertical Hanging Flower ",
                        "sellerId": {
                            "_id": "5b5c747d8209982630bbffe5",
                            "pin": "110008",
                            "companyName": "Retail ",
                            "firstName": "Houzzcart ",
                            "lastName": "Singh "
                        },
                        "slug": "hanging-flower-wisteria-by-houzzcart-1",
                        "measurementUnit": "Pc ",
                        "commission": 10,
                        "offerId": {
                            "discount": 52,
                            "title": "Discount 52%"
                        },
                        "sellingPrice": 110,
                        "discountedPrice": "53",
                        "discount": 52,
                        "finalPrice": 62,
                        "gstTax": "10"
                    },
                    "quantity": 1,
                    "gst": 18,
                    "discount": 52,
                    "commission": 10,
                    "__v": 0,
                    "createdAt": "2018-10-23T09:54:16.534Z",
                    "updatedAt": "2018-10-23T09:54:16.534Z"
                },
                {
                    "_id": "5bceefc85bba4826d22f7437",
                    "paymentId": "",
                    "cartId": [
                        "5bc497e23dab3b5513f101ec"
                    ],
                    "status": "init",
                    "cod": false,
                    "userId": "5b711d6572b22b0ecc361345",
                    "productId": "5ba103c96d529f30df60dd67",
                    "productDetails": {
                        "_id": "5ba103c96d529f30df60dd67",
                        "stock": 4000,
                        "gst": 18,
                        "images": [
                            "http://localhost:3000/uploads/1537969242350.png"
                        ],
                        "visible": true,
                        "title": "16074 - 3",
                        "subTitle": "CANYON RANCH",
                        "basePrice": 65,
                        "measurementUnit": "26.2",
                        "weight": 18,
                        "slug": "16074-3-1",
                        "sellerId": {
                            "_id": "5b6a97cf23d79b3625a5be56",
                            "pin": "123455",
                            "companyName": "Linkites",
                            "firstName": "Piyush",
                            "lastName": "Jain",
                            "pickupAddress": {
                                "_id": "5b92251d35c5ba2936dc4442",
                                "pickupLocationId": "",
                                "pickupLocation": "VIRGIN SURFACE",
                                "name": "Gunjan Singh",
                                "email": "houzzcart@gmail.com",
                                "addressLine1": "W-6, West Patel Nagar, Basement Opp. Metro Pilar No. 234 ,",
                                "addressLine2": " New Delhi, DELHI ,India ",
                                "city": "New Delhi",
                                "state": "Delhi",
                                "pincode": 110015,
                                "phone": 8368069699,
                                "companyId": "",
                                "status": "Active",
                                "createdAt": "2018-09-07T07:13:33.916Z",
                                "__v": 0
                            }
                        },
                        "commission": 10,
                        "offerId": {
                            "discount": 30,
                            "title": "Flat 30%"
                        },
                        "sellingPrice": 200,
                        "discountedPrice": "140",
                        "discount": 30,
                        "finalPrice": 495,
                        "gstTax": "25"
                    },
                    "quantity": 3,
                    "gst": 18,
                    "discount": 30,
                    "commission": 10,
                    "__v": 0,
                    "createdAt": "2018-10-23T09:54:16.535Z",
                    "updatedAt": "2018-10-23T09:54:16.535Z"
                },
                {
                    "_id": "5bceefc85bba4826d22f7438",
                    "paymentId": "",
                    "cartId": [
                        "5bcade6cb5669e200ab5a301"
                    ],
                    "status": "init",
                    "cod": false,
                    "userId": "5b711d6572b22b0ecc361345",
                    "productId": "5bacd386b02b216aa29132da",
                    "productDetails": {
                        "_id": "5bacd386b02b216aa29132da",
                        "stock": 100,
                        "gst": 18,
                        "images": [
                            "http://houzzcart.com/uploads/1538054447865.png",
                            "http://houzzcart.com/uploads/1538054464640.png",
                            "http://houzzcart.com/uploads/1538054464638.png",
                            "http://houzzcart.com/uploads/1538054464639.png"
                        ],
                        "visible": true,
                        "title": "Avian Solid Wood Bar Cabinet In Warm Chestnut Finish",
                        "subTitle": "Bar Furniture",
                        "basePrice": 5000,
                        "measurementUnit": "Per Pc",
                        "weight": 60,
                        "slug": "avian-solid-wood-bar-cabinet-in-warm-chestnut-finish-3283",
                        "sellerId": {
                            "_id": "5b5c747d8209982630bbffe5",
                            "pin": "110008",
                            "companyName": "Retail ",
                            "firstName": "Houzzcart ",
                            "lastName": "Singh "
                        },
                        "commission": 10,
                        "sellingPrice": 10000,
                        "offerId": {
                            "discount": 54,
                            "title": "Furniture 1"
                        },
                        "discountedPrice": "4600",
                        "discount": 54,
                        "finalPrice": 5428,
                        "gstTax": "828"
                    },
                    "
                ],
                "status": "placed",
                "cod": true,
                "_id": "5bd07333a17e9a410326cbce",
                "userId": {
                    "activity": {
                        "loginCount": 0,
                        "dateEstablished": "2018-08-13T05:55:49.873Z",
                        "lastLogin": "2018-08-13T05:55:49.873Z"
                    },
                    "env": "development",
                    "isAuth": false,
                    "isEmailVerified": true,
                    "isAdminUser": false,
                    "isRetailer": false,
                    "isServiceProvider": false,
                    "mobile": "",
                    "isEmailSend": false,
                    "profile_picture": "http://localhost:3000/uploads/1538032590714.png",
                    "street": "muktanand parisar",
                    "otp": 0,
                    "city": "Dhamnod",
                    "state": "m.p.",
                    "pin": "1231231313",
                    "GSTIN": "",
                    "registerNumber": "",
                    "shopName": "",
                    "mobileNumber": "9753945608",
                    "companyName": "Amba villa",
                    "companyAddress": "",
                    "address": "asdsadsadasd",
                    "firstName": "v",
                    "lastName": "p",
                    "prime": false,
                    "isBlocked": false,
                    "status": 10,
                    "deliveryAddress": [
                        "5b8780d566650e11bf8524e0",
                        "5b8e24de8dbd5413a6b7c0ec",
                        "5b8e52623fc01b1fc1f97fe2",
                        "5bc09382e250fe310785c326",
                        "5bc09387e250fe310785c327",
                        "5bc57dec90e14d176e3311e7",
                        "5bc57df890e14d176e3311e8",
                        "5bc6d084e1f15e105951c3cc",
                        "5bc6d115e1f15e105951c3cd",
                        "5bc6d156e1f15e105951c3ce",
                        "5bc6d15ee1f15e105951c3cf",
                        "5bc6d227e1f15e105951c3d0",
                        "5bc6d2d0e1f15e105951c3d1",
                        "5bc6d364e1f15e105951c3d2",
                        "5bc6d376e1f15e105951c3d3",
                        "5bc6d489e1f15e105951c3d4",
                        "5bc6d4e1e1f15e105951c3d5",
                        "5bc6d63ce1f15e105951c3d6",
                        "5bc6dbcce1f15e105951c3d7",
                        "5bc6dc1ee1f15e105951c3d8",
                        "5bc6e316e1f15e105951c3d9",
                        "5bc82e4056c24e1feecf4fe0",
                        "5bcb1138a42ad42fc5852480",
                        "5bd15d690a7ac510fc4260d0",
                        "5bd1c2b19fcf0536d10447ea",
                        "5bdff0baba8bb816fda10a48"
                    ],
                    "inviteLink": null,
                    "deviceType": null,
                    "deviceToken": null,
                    "_id": "5b711d6572b22b0ecc361345",
                    "isAdminRead": false,
                    "email": "er.vikaspatidar@gmail.com",
                    "createdAt": "2018-08-13T05:55:49.885Z",
                    "updatedAt": "2018-11-05T07:26:50.776Z",
                    "__v": 0,
                    "shippingAddress": []
                },
                "orderId": "OHC18102417",
                "totalDiscount": 336,
                "totalGST": 40,
                "totalProduct": 1,
                "totalBasePrice": 999,
                "totalCommision": 10,
                "deliveryTotal": 0,
                "subTotal": 336,
                "totalPayableAmount": 376,
                "shippingAddress": {
                    "name": "vikas patidar",
                    "add": "sh sh",
                    "city": "inodre",
                    "state": "mp",
                    "country": "India",
                    "pin": 452001,
                    "phone": 9753945608
                },
                "createdAt": "2018-10-24T13:27:15.456Z",
                "updatedAt": "2018-10-24T13:27:19.003Z",
                "__v": 0
            }
        ]
    }

我要过滤itemId数组中的数据(这是对 productsorderitems 集合进行查找的结果),以仅使那些具有Sellerstrong为 5b5c747d8209982630bbffe5 的对象成为对象>,(sellerId字段位于productDetails中),这是我正在使用的命令:

[
        {
          $match: {orderId: "OHC18102417"}
        },
        {
          $lookup: {
            from:"productsorderitems",
            localField: "userId",
            foreignField: "userId",
            as: "itemId"
          }
        },
        {
          $lookup: {
            from:"users",
            localField: "userId",
            foreignField: "userId",
            as: "userId"
          }
        },
        { $sort : { "_id": -1 } }
      ]

我已经尝试过其他帖子上的解决方案,但是不幸的是,它们都没有用,谢谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您是否尝试过在$lookup中使用pipeline?这些行中的一些内容:

{
  $lookup: {
    from: "productsorderitems",
    pipeline: [{
      $match: {
        'productDetails.sellerId._id': ObjectId('5b5c747d8209982630bbffe5')
      }
    }],
    as: "itemId"
  }
}

答案 1 :(得分:1)

您可以使用mongodb 3.6 $lookup语法来过滤$lookup管道中的文档。

[
  { "$match": { "orderId": "OHC18102417" }},
  { "$lookup": {
    "from": "productsorderitems",
    "let": { "userId": "$userId" },
    "pipeline": [
      { "$match": {
        "$expr": { "$eq": ["$userId", "$$userId"] },
        "sellerId._id": "5b5c747d8209982630bbffe5"
      }}
    ],
    "as": "itemId"
  }}
]

或者您可以使用$filter聚合

[
  { "$match": { "orderId": "OHC18102417" }},
  { "$lookup": {
    "from":"productsorderitems",
    "localField": "userId",
    "foreignField": "userId",
    "as": "itemId"
  }},
  { "$addFields": {
    "itemId": {
      "$filter": {
        "input": "$itemId",
        "as": "item",
        "cond": { "$eq": ["$$item.sellerId._id", "5b5c747d8209982630bbffe5"] }
      }
    }
  }}
]