gpt4 book ai didi

mongodb - 如何使用具有多个状态的集合在 mongodb 中获得第二高状态

转载 作者:行者123 更新时间:2023-12-04 01:12:52 25 4
gpt4 key购买 nike

    /* 1 */
{
"_id" : ObjectId("5f0b6699486ea60a2a9c62fh"),
"EventDatetime" : "2020-07-12T19:38:00.653",
"Eventstatus" : "05",
"DelNumber" : "0703676929",
"DELIVERY_ITEM" : [
{
"Product_Code" : "123",
"Product_Name" : "utensils",
"Shipment_Quantity" : "12",
},
{
"Product_Code" : "456",
"Product_Name" : "clothes",
"Shipment_Quantity" : "10",
}
]
}
{
"_id" : ObjectId("5f0b6699486ea60a2a9c62fi"),
"EventDatetime" : "2020-07-12T19:38:00.653",
"Eventstatus" : "03",
"DelNumber" : "0703676929",,
"DELIVERY_ITEM" : [
{
"Product_Code" : "123",
"Product_Name" : "utensils",
"Shipment_Quantity" : "12",
},
{
"Product_Code" : "456",
"Product_Name" : "clothes",
"Shipment_Quantity" : "10",
}
]
}
{
"_id" : ObjectId("5f0b6699486ea60a2a9c62fg"),
"EventDatetime" : "2020-07-12T19:38:00.653",
"Eventstatus" : "02",
"DelNumber" : "0703676929",
"DELIVERY_ITEM" : [
{
"Product_Code" : "123",
"Product_Name" : "utensils",
"Shipment_Quantity" : "12",
},
{
"Product_Code" : "456",
"Product_Name" : "clothes",
"Shipment_Quantity" : "10",
}
]
}
/*2*/

{
"_id" : ObjectId("5f0b6699486ea60a2a9c62fa"),
"EventDatetime" : "2020-07-12T19:38:00.653",
"Eventstatus" : "04",
"DelNumber" : "0703676928",
"DELIVERY_ITEM" : [
{
"Product_Code" : "123",
"Product_Name" : "utensils",
"Shipment_Quantity" : "12",
},
{
"Product_Code" : "456",
"Product_Name" : "clothes",
"Shipment_Quantity" : "10",
}
]
}
{
"_id" : ObjectId("5f0b6699486ea60a2a9c62fb"),
"EventDatetime" : "2020-07-12T19:38:00.653",
"Eventstatus" : "02",
"DelNumber" : "0703676928",
"DELIVERY_ITEM" : [
{
"Product_Code" : "123",
"Product_Name" : "utensils",
"Shipment_Quantity" : "12",
},
{
"Product_Code" : "456",
"Product_Name" : "clothes",
"Shipment_Quantity" : "10",
}
]
}
{
"_id" : ObjectId("5f0b6699486ea60a2a9c62fc"),
"EventDatetime" : "2020-07-12T19:38:00.653",
"Eventstatus" : "01",
"DelNumber" : "0703676928",
"DELIVERY_ITEM" : [
{
"Product_Code" : "123",
"Product_Name" : "utensils",
"Shipment_Quantity" : "12",
},
{
"Product_Code" : "456",
"Product_Name" : "clothes",
"Shipment_Quantity" : "10",
}
]
}

我正在收集订单。将其视为电子商务网站。现在,订单可以有多个状态 00,01,02,03,04 和 05 。 05指的是订单的实时跟踪。所以我的 delno.1 可以有状态 01,02,05,05,03,05,05。我的问题是如何获得我运行的查询将 03 作为最新状态而不是 05。此外,当状态达到 04 时,我不需要检索该记录。任何帮助都会很棒!!

最佳答案

您必须首先删除所有状态为 04 的文档。然后对同一集合进行查找以获取剩余的文档。展开它并过滤掉状态为 05 的文档。

[
{
//Group records by DelNumber
$group: {
_id: '$DelNumber',
EventStatuses: {
$addToSet: '$Eventstatus'
}
}
}, {
// Remove any with status 04
$match: {
EventStatuses: {
$ne: '04'
}
}
}, {
// Looksup on same collection to get full document
$lookup: {
from: 'collection',
localField: '_id',
foreignField: 'DelNumber',
as: 'matched'
}
}, {
// Unwind the matched records
$unwind: {
path: '$matched'
}
}, {
$replaceRoot: {
newRoot: '$matched'
}
}, {
// Remove any record with status 05
$match: {
Eventstatus: {
$ne: '05'
}
}
}
]

关于mongodb - 如何使用具有多个状态的集合在 mongodb 中获得第二高状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64273948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com