我已經收集了門票,我需要獲取在日、周和月出售的門票數量。下面是結構
{
"_id": "9917aed79fcc274bdefgj",
"ticketId": 100,
"createdAt": "2021-11-08T08:34:39.697 00:00"
},
{
"_id": "9917aed79fcc274bdefgj",
"ticketId": 101,
"createdAt": "2021-11-07T08:34:39.697 00:00"
},
{
"_id": "9917aed79fcc274bdefgj",
"ticketId": 102,
"createdAt": "2021-11-06T08:34:39.697 00:00"
},
{
"_id": "9917aed79fcc274bdefgj",
"ticketId": 103,
"createdAt": "2021-11-05T08:34:39.697 00:00"
},
{
"_id": "9917aed79fcc274bdefgj",
"ticketId": 104,
"createdAt": "2021-11-04T08:34:39.697 00:00"
},
{
"_id": "9917aed79fcc274bdefgj",
"ticketId": 105,
"createdAt": "2021-11-03T08:34:39.697 00:00"
},
{
"_id": "9917aed79fcc274bdefgj",
"ticketId": 106,
"createdAt": "2021-11-02T08:34:39.697 00:00"
},
{
"_id": "9917aed79fcc274bdefgj",
"ticketId": 107,
"createdAt": "2021-11-01T08:34:39.697 00:00"
}
我是 MongoDB 的新手,我嘗試使用 project 或 match 來匯出日、周或月,但是我沒有成功,因此無法分享我的大部分作業。感謝任何幫助
更新:
查詢 08-Nov
日的預期輸出:1
周:1
月:8
輸出說明:
天:1 — 沒有。在給定日期
周售出的門票數量:1 — 沒有。給定周售出的門票數量(從周一開始,僅售出 1 張門票)
月:8 — 沒有。在指定月份售出的門票中,已售出 8 張門票
uj5u.com熱心網友回復:
詢問
- 將您想要的日期放在第一組中,如果您想要當前日期,請保留
$$NOW具有當前日期的系統變數 - 年/月/周/日的 4 倍相同代碼
- 這里在組之前過濾,以具有與
$$NOW當前日期相同的年或月或周或日。
*我過去$isoWeek一周從周一$week開始,從周日開始,你可以改變它們
測驗代碼在這里
aggregate(
[{"$set": {"date": "$$NOW"}},
{"$facet":
{"year":
[{"$match":
{"$expr": {"$eq": [{"$year": "$date"}, {"$year": "$createdAt"}]}}},
{"$group": {"_id": null, "count": {"$sum": 1}}},
{"$unset": ["_id"]}],
"month":
[{"$match":
{"$expr":
{"$and":
[{"$eq": [{"$year": "$date"}, {"$year": "$createdAt"}]},
{"$eq": [{"$month": "$date"}, {"$month": "$createdAt"}]}]}}},
{"$group": {"_id": null, "count": {"$sum": 1}}},
{"$unset": ["_id"]}],
"week":
[{"$match":
{"$expr":
{"$and":
[{"$eq": [{"$year": "$date"}, {"$year": "$createdAt"}]},
{"$eq": [{"$isoWeek": "$date"}, {"$isoWeek": "$createdAt"}]}]}}},
{"$group": {"_id": null, "count": {"$sum": 1}}},
{"$unset": ["_id"]}],
"day":
[{"$match":
{"$expr":
{"$and":
[{"$eq": [{"$year": "$date"}, {"$year": "$createdAt"}]},
{"$eq":
[{"$dayOfYear": "$date"}, {"$dayOfYear": "$createdAt"}]}]}}},
{"$group": {"_id": null, "count": {"$sum": 1}}},
{"$unset": ["_id"]}]}},
{"$set":
{"year":
{"$cond":
[{"$eq": ["$year", []]}, 0, {"$arrayElemAt": ["$year.count", 0]}]},
"month":
{"$cond":
[{"$eq": ["$month", []]}, 0, {"$arrayElemAt": ["$month.count", 0]}]},
"week":
{"$cond":
[{"$eq": ["$week", []]}, 0, {"$arrayElemAt": ["$week.count", 0]}]},
"day":
{"$cond":
[{"$eq": ["$day", []]}, 0, {"$arrayElemAt": ["$day.count", 0]}]}}}])
結果
(“日期”為 11 月 9 日,其中包含我得到的資料)
[
{
"day": 1,
"month": 8,
"week": 1,
"year": 8
}
]
uj5u.com熱心網友回復:
在這里測驗
db.collection.aggregate([
{
"$project": {
"createdAtWeek": {
"$week": "$createdAt"
},
"createdAtMonth": {
"$month": "$createdAt"
},
"createdAtDay": {
"$dayOfMonth": "$createdAt"
}
}
},
{
"$group": {
"_id": {
createdAtWeek: "$createdAtWeek",
createdAtMonth: "$createdAtMonth",
createdAtDay: "$createdAtDay"
},
count: {
$sum: 1
}
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/354249.html
上一篇:如何在MongoDbDriver中的IList中包含特定欄位
下一篇:Rust更新mongo檔案
