資料型別如下。
_id: ObjectId
details: array
0: Object
salesManagerId: ObjectId
createdAt: Date
資料型別如下。
我想要的是,
日期:按日期分組。通過按年、月和日分組的結果按降序排序。作為排序結果,應該按照與 20211020, 20211019 相同的順序進行排序。
Sales:應該是按日期分組的日期包含多少個_id。我認為它相當于mysql中的SELECT COUNT(*)。
自有銷售量:在分組計數中,應輸出包含明細的數字。但是,如果 details.salesManagerId 中包含特定的 obejctId,則應將其排除。
我期待下表。
Date Sales Own Sales Volume
20211020 30 11
20211019 15 2
20211018 23 3
.
.
.
特別是,我很難按日期分組。
uj5u.com熱心網友回復:
$group
db.collection.aggregate([
{
"$group": {
"_id": {
year: {
$year: "$createdAt"
},
month: {
$month: "$createdAt"
},
day: {
$dayOfMonth: "$createdAt"
}
},
"Sales": {
"$sum": 1
},
"Own Sales Volume": {
"$sum": {
$cond: [
{
$and: [
{
$isArray: "$details"
},
{
$gt: [
{
"$size": "$details"
},
0
]
}
]
},
1,
0
]
}
}
}
}
])
mongoplayground
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/329199.html
標籤:MongoDB
上一篇:req.body是空的,為什么?
