【发布时间】:2019-10-23 10:05:53
【问题描述】:
有很多问题/答案听起来与我正在寻找的完全一样,但我找不到一个真正适合我的问题/答案。
样本数据:
{
"_id": "5daeb61790183fd4d4361d6c",
"orderMessageId": "7563_21",
"orderId": "OS00154",
"orderEntryDate": "2019-06-17T00:00:00.000Z",
"typeOfOrder": "ORD",
"express": false,
"name1": "xxx",
"name2": "xxx",
"name3": " ",
"contact": "IN KOMMISSION",
"street": "xxx",
"city": "xxx",
"zipcode": "1235",
"country": "xx",
"customerId": "51515",
"lnatMarketCode": "Corporate - Regulatory",
"shipmentCarrier": "ABH",
"typeOfShipment": "83",
"typeOfShipmentDescr": "xxx",
"orderTextfield": " ",
"orderTextfield02": " ",
"text2": " ",
"LinVw": [
{
"orderLineMessageId": "OS05451",
"orderLineId": 5,
"articleId": "19200",
"articleDescription": "xxx",
"productId": "OS1902",
"productDescription": "xxx",
"baseQuantityUnit": "EA",
"quantityOrdered": 2,
"isbn": "978357468",
"issn": " ",
"orderSubmissionDate": "2019-06-06T00:00:00.000Z",
"customerPurchaseOrderId": "728188175",
"lnatCustomerIdAtSupplier": " ",
"supplierDeliveryNoteId": " ",
"fulfillmentContactName": "xxxx",
"customerVatRegistrationCode": "AT4151511900",
"listPriceInclVat": 21.4955,
"text": " ",
"orderResponses": [
{
"orderMessageId": "7718677_1",
"orderLineMessageId": "OS0000015451",
"orderId": "OS000154",
"orderLineId": 5,
"articleId": "1911200",
"quantity": 2,
"quantityNotShipped": 0,
"reasonForNotShippedCode": null,
"reasonForNotShipped": null,
"shipmentDate": "2019-10-04T00:00:00.000Z",
"deliveryNoteId": null,
"trackingIds": [
{
"trackingId": null,
"quantityRefToTracking": "2",
"weightRefToTracking": "0.0"
}
],
"type": "orderresponse",
"filepath": "xxxORDERRESP_20191004131209.xml",
"_id": "OS005451"
},
{
"orderMessageId": "753_21",
"orderLineMessageId": "OS015451",
"orderId": "O00154",
"orderLineId": 5,
"articleId": "100200",
"quantity": 0,
"quantityNotShipped": 2,
"reasonForNotShippedCode": "01",
"reasonForNotShipped": "Out of Stock",
"shipmentDate": null,
"deliveryNoteId": null,
"trackingIds": [
{
"trackingId": null,
"quantityRefToTracking": "0",
"weightRefToTracking": "0.0"
}
],
"type": "orderresponse",
"filepath": "xxxxORDERRESP_20190618161529.xml",
"_id": "OS0000015451"
}
]
}
],
"filepath": "xxxxxORDER_7539563_20190618_071522.xml"
}
我要匹配所有文档,其中所有文档在数组LinVw中,匹配如下条件:
{'$or': [{'LinVw.orderResponses': {'$exists': False}}, {'LinVw.orderResponses.shipmentDate': {'$type': 10}}]}
换句话说:我想匹配文档,如果数组 LinVw.orderResponses 不存在,或者它只包含文档,并且没有有效的 shippingDate。
目前我有这个(使用 pymongo):
result = order_collection.aggregate([
{"$unwind": "$LinVw"},
{"$match": {'$or': [{'LinVw.orderResponses': {'$exists': False}}, {'LinVw.orderResponses.shipmentDate': {'$type': 10}}]}}
])
但这当然并不认为 LinVw.orderResponses 中的所有文档都应该符合条件。
我们那里的大多数示例都没有处理这种嵌套,我无法相应地重写它们。
我将不胜感激。
【问题讨论】:
-
你能客观解释一下吗?
-
@niranjanharpale 对不起,我没有关注。你要我解释什么?
标签: mongodb mongodb-query aggregation-framework