问题
I have the following table :-
id order_number product_number order_status schedule_datetime
1 001 001.1 SUCCESS 20180103
2 001 001.2 SUCCESS 20180102
3 111 111.1 SUCCESS 20171225
4 111 111.2 SUCCESS 20171224
5 222 222.1 INPROGRESS 20171122
6 222 222.2 ON_HOLD 20171121
7 222 222.3 PARTLY_SUCCESS 20171121
What i am trying here, if any of the product_number matches the status given in the query, it should display the year and list of order_numbers.
Input 1: order_status = SUCCESS, below output should come
{
"order_statuses" : [
"SUCCESS"
],
"year" : "2018",
"order_number" : "001"
},
{
"order_statuses" : [
"SUCCESS"
],
"year" : "2017",
"order_number" : "111"
}
Input 2: order_status = PARTLY_SUCCESS, below output should come
{
"order_statuses" : [
"PARTLY_SUCCESS",
"ON_HOLD",
"INPROGRESS"
],
"year" : "2017",
"order_number" : "222"
} With the help of this link convert mysql query contains sum and group_concat to mongodb query I have tried to get the desired result, but below query gives error "The field name '$addFields' cannot be an operator name". As i don't want to display count i tried adding to separate group but it didn't help.
db.order_summary.aggregate([
{"$project":{
"schedule_datetime":1,
"order_number ":1,
"order_status":{"$ifNull":["$order_status",""]}
}},
{"$group":{
"_id":{
"order_number ":"$order_number ",
"order_status":"$order_status"
},
"study_date":{"$first": "$study_date"}
}},
{"$sort":{"_id.order_status": 1}},
{"$group":{
"_id":{
"order_number ":"$_id.order_number "
},
"study_date":{"$first": "$study_date"},
"order_status":{"$push": "$_id.order_status"}
}},
{"$group":{
"_id":{
"$substr":["$study_date",0,4]
},
"count":{
"$sum":{
"$cond": [
{"$in": ["$order_status",[["SUCCESS"],["INPROGRESS","SUCCESS"]]]},
1,0
]
}
},
"order_numbers":{"$push":"$_id.order_number "},
"$addFields":{"$size":"$order_numbers"},
}},
{"$match":{"order_numbers":{"$gt":0}}},
{"$sort":{"_id":-1}}
])
Could you please help to display the year and list of order_numbers as per above output, Thanks!
回答1:
You can try below aggregation query.
First $group
to get distinct order number and status combination and pick the latest time stamp.
Second $group
to get the grouping by year and status and pick all the order numbers.
$match
to compare the status group against the input status.
db.order_summary.aggregate([
{"$project":{
"schedule_datetime":1,
"order_number ":1,
"order_status":{"$ifNull":["$order_status",""]
}
}},
{"$sort":{"schedule_datetime":-1}},
{"$group":{
"_id":{
"order_number":"$order_number",
"order_status":"$order_status"
},
"schedule_datetime":{"$first":"$schedule_datetime"}
}},
{"$group":{
"_id":{
"year":{"$substr":["$schedule_datetime",0,4]},
"order_number":"$_id.order_number"
},
"order_statuses":{"$push":"$_id.order_status"}
}},
{"$redact": {"$cond":[{"$in": ["SUCCESS","$order_statuses"]},"$$KEEP","$$PRUNE"]}},
{"$sort":{"_id.year":-1}},
{"$project":{"_id":0, "year":"$_id.year","order_number":"$_id.order_number", "order_statuses":1 }},
])
来源:https://stackoverflow.com/questions/48299177/compare-multiple-values-inside-aggregation-and-display-result-in-array-in-mongod