使用匹配条件过滤查询结果? MongoDB

时间:2019-09-08 18:31:20

标签: mongodb collections mongodb-query aggregation-framework

我完全不知道应该如何使用管道过滤查找的结果数组,这是代码

{
    "_id" : ObjectId("5d73d591b35c943a201837e2"),
    "itemName" : "Vape",
    "itemSellingPrice" : "350",
    "itemPurchasePrice" : "300",
    "itemAveragePurchasePrice" : "133.33333333333334",
    "itemBaseUnit" : "Unit 2",
    "itemReorderPoint" : "100",
    "itemTotalQuantity" : "300",
    "itemSumQuantity" : "500",
    "itemLocation" : "1",
    "itemSubLocation" : "sub loc 1",
    "itemDateCreated" : ISODate("2019-09-07T16:06:41.521Z"),
    "itemID" : 88,
    "__v" : 0,
    "salesData" : [ 
        {
            "_id" : ObjectId("5d73e23ed8422d2ba42049b4"),
            "salesOrderCustomerName" : "Manong Puring",
            "salesOrderInvoiceNumber" : "1123",
            "salesOrderAddress" : "Jan lang",
            "salesOrderPaymentStatus" : "Open",
            "salesOrderTotalPaid" : "0",
            "salesOrderTotalAmount" : "2800",
            "salesOrderDiscrepancyAmount" : "2800",
            "salesOrderItemList" : [ 
                {
                    "_id" : ObjectId("5d73e23ed8422d2ba42049b5"),
                    "salesOrderSelectedItem" : "Vape",
                    "salesOrderAverage" : "133.33333333333334",
                    "salesOrderNewPrice" : "350",
                    "salesOrderPurchasePrice" : "300",
                    "salesOrderQuantity" : "8",
                    "salesOrderSubTotal" : "2800"
                }
            ],
            "salesOrderDateCreated" : ISODate("2019-09-07T16:00:00.000Z"),
            "salesOrderSubLocation" : "sub loc 1",
            "salesOrderLocation" : "1",
            "salesOrderID" : 62,
            "__v" : 0
        }, 
        {
            "_id" : ObjectId("5d73e37164ade31b40775038"),
            "salesOrderCustomerName" : "Manong Puring",
            "salesOrderInvoiceNumber" : "123",
            "salesOrderAddress" : "Jan lang",
            "salesOrderPaymentStatus" : "Open",
            "salesOrderTotalPaid" : "0",
            "salesOrderTotalAmount" : "350",
            "salesOrderDiscrepancyAmount" : "350",
            "salesOrderItemList" : [ 
                {
                    "_id" : ObjectId("5d73e37164ade31b40775039"),
                    "salesOrderSelectedItem" : "Vape",
                    "salesOrderAverage" : "133.33333333333334",
                    "salesOrderNewPrice" : "350",
                    "salesOrderPurchasePrice" : "300",
                    "salesOrderQuantity" : "1",
                    "salesOrderSubTotal" : "350"
                }
            ],
            "salesOrderDateCreated" : ISODate("2019-09-07T16:00:00.000Z"),
            "salesOrderSubLocation" : "sub loc 2",
            "salesOrderLocation" : "1",
            "salesOrderID" : 63,
            "__v" : 0
        }
    ]
}

我只想使用salesOrderSubLocation: "sub loc 1"获取salesData 但它也显示带有子位置2的数据。搜索了一段时间,但找不到我的确切问题。

这是我的查询

db.getCollection('itemmodels').aggregate(

{ '$match': { itemName: 'Vape' } },
{ '$lookup':
     { 
       from: 'itemmodels',
       let: { "itemName": "$itemName" },
       pipeline: [
        { $match: {
          "salesOrderSubLocation": "sub loc 1",
          "salesOrderItemList.salesOrderSelectedItem": "$$itemName"
        }
        }
      ],
       as: 'salesData' 
    } 
})

有什么主意吗?我不想在前端过滤结果,因为这会在将来导致大量数据出现问题。

0 个答案:

没有答案