mongodb - 如何在具有多个状态的集合中获得 mongodb 中的第二高状态
问题描述
/* 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 时,我不需要检索该记录。任何帮助都会很棒!
解决方案
你可以试试,
$match
您的条件,DelNumber 是“0703676929”并且 Eventstatus 不等于 4$addFields
使用在整数字段中添加 Eventstatus$toInt
$sort
按状态字段降序排列$skip
在第二页 (1) 因为第一个是 5 状态$limit
单条记录
db.collection.aggregate([
{
$match: {
DelNumber: "0703676929",
Eventstatus: { $ne: "04" }
}
},
{ $addFields: { status: { $toInt: "$Eventstatus" } } },
{ $sort: { status: -1 } },
{ $skip: 1 },
{ $limit: 1 }
])
第二种方式,您可以在第 5 种状态下使用修复条件,
$match
您的条件,DelNumber 是“0703676929”并且 Eventstatus 不等于 4th 和 5th$addFields
使用在整数字段中添加 Eventstatus$toInt
$sort
按状态字段降序排列$limit
单条记录
db.collection.aggregate([
{
$match: {
DelNumber: "0703676929",
Eventstatus: { $nin: ["05", "04"] }
}
},
{ $addFields: { status: { $toInt: "$Eventstatus" } } },
{ $sort: { status: -1 } },
{ $limit: 1 }
])
推荐阅读
- docker - 有没有一种好的和安全的方式来允许非 root 用户启动一个 docker 镜像?
- mongodb - MongoDB 聚合在索引字段上很慢
- java - JList - 检索项目
- amazon-web-services - 您能否在 CloudFormation 中为 AWS 特定参数提供可选参数?
- django - Django模型形式:m2m的反面不保存
- php - 我需要帮助才能在 WordPess 中使用以下代码进行 Paypal 自适应付款
- php - tinyint 类型的 MySQL 字符串值 - PHP PDO 构建
- javascript - 如果使用 jQuery/javascript ajax 在响应中找到“Content-Disposition”,则自动下载文件
- android - 具有自定义视图的 objectanimator
- excel - 将excel中的日期格式从dd.mmm转换为dd.mm