我正在努力了解 MongoDB 以及聚合和組。到目前為止,我已經花了大約 3 天。
我的源資料看起來像......
{
"formName" : "my form",
"updatedAt" : "2021-11-02T13:29:00.123Z",
},
{
"formName" : "another form",
"lastUpdated" : "2021-10-01T13:29:00.123123",
},
請注意,可能存在不同的日期名稱,盡管這些是唯一的區別。
我正在嘗試實作...的輸出
{
"_id": null,
"text": "my form", (NOTE: This is the formName)
"children": [{
"text" : 2021, (This is the year part of the updated)
"children" : [
{"text" : 1}, (These are the month part of the updated)
{"text" : 2},
{"text" : 3},
{"text" : 4}
]
},
]
}
所以,基本上是一棵樹,它有 formName,有幾年的子分支,有幾個月的子分支。
我嘗試了各種方法,很多都不起作用,例如 $groups 中嵌套的 $addToSet。
我已經接近了,但我無法解決它。
這是最接近的,但這不起作用。
db.FormsStore.aggregate( [
{$match:{myKey:"a guid to group my forms together"}},
{$project: {formName:1, lastUpdated:1, updatedAt:1}},
{
$group: {
_id: { formName: "$formName" },
Year: {$addToSet: {$year: {$dateFromString: { dateString: "$lastUpdated" }}}},
Month: {$addToSet: {$month: {$dateFromString: { dateString: "$lastUpdated" }}}},
}
},
{
$group: {
_id: { formName: "$_id.formName" },
Time: {$addToSet: {year: "$Year", month: "$Month"}}
}
}
]
)
其輸出顯示...
{
_id: { formName: 'One of my forms' },
Time: [
{
year: [ 2021 ],
month: [ 10, 11 ]
}
]
}
這將全部用于 C#
您的幫助將不勝感激。
uj5u.com熱心網友回復:
詢問
- 添加一個帶有日期格式的新欄位“日期”
- group first 更具體
(formname year)地將月份放入陣列中 - group 然后不太具體
(formname)地將年份放入陣列中
測驗代碼在這里
aggregate(
[{"$set":
{"date":
{"$cond":
["$updatedAt", {"$dateFromString": {"dateString": "$updatedAt"}},
{"$dateFromString": {"dateString": "$lastUpdated"}}]},
"updatedAt": "$$REMOVE",
"lastUpdated": "$$REMOVE"}},
{"$group":
{"_id": {"text": "$formName", "year": {"$year": "$date"}},
"children": {"$push": {"text": {"$month": "$date"}}}}},
{"$group":
{"_id": "$_id.text",
"children":
{"$push": {"text": "$_id.year", "children": "$children"}}}},
{"$set": {"text": "$_id", "_id": "$$REMOVE"}}])
編輯
波紋管也按年/月排序,并且每個 formName 只保留唯一的年/月。
- 不同之處在于通過 formName,year,month 進行分組以獲取唯一值(第一個 aacumulator 將僅獲取所有 3 個中相同的一個)
- replace-root(使第一個檔案成為 ROOT 檔案)
- 然后按這 3 個欄位排序(降序年份,升序月份)
- 團體
- 按 2 個欄位排序
- 最后一組
這里的測驗代碼
*mongoplaygroung 丟失了欄位的順序,在您的驅動程式上運行它也是為了確保
aggregate(
[{"$set":
{"date":
{"$cond":
["$updatedAt", {"$dateFromString": {"dateString": "$updatedAt"}},
{"$dateFromString": {"dateString": "$lastUpdated"}}]},
"updatedAt": "$$REMOVE",
"lastUpdated": "$$REMOVE"}},
{"$set": {"year": {"$year": "$date"}, "month": {"$month": "$date"}}},
{"$group":
{"_id": {"formName": "$formName", "year": "$year", "month": "$month"},
"doc": {"$first": "$$ROOT"}}},
{"$replaceRoot": {"newRoot": "$doc"}},
{"$sort": {"formName": 1, "year": -1, "month": 1}},
{"$group":
{"_id": {"text": "$formName", "year": "$year"},
"children": {"$push": {"text": "$month"}}}},
{"$sort": {"_id.text": 1, "_id.year": -1}},
{"$group":
{"_id": "$_id.text",
"children":
{"$push": {"text": "$_id.year", "children": "$children"}}}},
{"$set": {"text": "$_id", "_id": "$$REMOVE"}}])
有資料
[
{
"formName": "my form",
"updatedAt": "2021-11-02T23:30:15.123Z"
},
{
"formName": "my form",
"updatedAt": "2021-10-02T23:30:15.123Z"
},
{
"formName": "my form",
"updatedAt": "2020-06-02T23:30:15.123Z"
},
{
"formName": "my form",
"updatedAt": "2020-07-02T23:30:15.123Z"
},
{
"formName": "another form",
"updatedAt": "2021-10-01T23:30:15.123Z"
},
{
"formName": "another form",
"updatedAt": "2021-10-01T23:30:15.123Z"
},
{
"formName": "another form",
"updatedAt": "2021-09-01T23:30:15.123Z"
},
{
"formName": "another form",
"updatedAt": "2021-08-01T23:30:15.123Z"
},
{
"formName": "another form",
"updatedAt": "2020-10-01T23:30:15.123Z"
}
]
我得到了結果
[{
"children": [
{
"text": 2021,
"children": [
{
"text": 10
},
{
"text": 11
}
]
},
{
"text": 2020,
"children": [
{
"text": 6
},
{
"text": 7
}
]
}
],
"text": "my form"
},
{
"children": [
{
"text": 2021,
"children": [
{
"text": 8
},
{
"text": 9
},
{
"text": 10
}
]
},
{
"text": 2020,
"children": [
{
"text": 10
}
]
}
],
"text": "another form"
}]
uj5u.com熱心網友回復:
我繼續研究它,雖然這也不太正確(還),但這是我想出的。
db.FormsStore.aggregate([
{$project: {formName:1, lastUpdated:1, updatedAt:1}},
{
$group: {
_id: {formName: "$formName", Year: {$year: {$dateFromString: { dateString: "$updatedAt" }}}, Month: {$month: {$dateFromString: { dateString: "$updatedAt" }}}},
}
},
{
$group: {
_id: {formName: "$_id.formName", Year: "$_id.Year"},
Months: {$addToSet: {Text: "$_id.Month"}}
}
},
{
$group: {
_id: "$_id.formName", Children: {$addToSet: {Text: "$_id.Year", Children: "$Months"}},
}
}
])
獲取第一組中的所有資料,然后在第二組中創建一個帶有月份的集合,然后創建一個帶有年份的集合,并將月份添加到第三組中的每一年。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346603.html
