[英]MongoDB Aggregate Array of Objects
我有許多名為“項目”的文件,格式如下:
Item = {
"_id" : ObjectId("6059f025fb8378a294180cc3"),
"name" : "My Name",
"category" : "My Category",
"marketData" : {
"currency" : "€",
"history" : [
{
"timestamp" : 1,
"price" : 1.1,
"volume" : 10
},
{
"timestamp" : 3,
"price" : 2.2,
"volume" : 20
},
]
}
}
歷史有不同的長度,每個文檔都有不同的時間戳,有些文檔會共享幾個時間戳。
我需要每個類別的每個時間戳的價格和數量的總和。 像這樣:
Categories =
[{
"_id",
"name": "My Category",
"marketData": {
"currency": "€",
"history": [
{
"timestamp": 1,
"price": "sum of prices of all items at timestamp 1",
"volume": "sum of volume of all items at timestamp 1"
},
...
]
}
},
...
]
我已經試過了,但它說SyntaxError: invalid property id
db.items.aggregate([
{ $unwind: "$marketData.history" },
{
$group: {
_id: "$marketData.history.timestamp",
price: { $sum:"$marketData.history.price" }
},
{
$group: {
_id:null,
price: {
$push: { timestamp: "$timestamp", price:"$price" }
}
}
},
{
$project: {
marketData.history:1,
_id:0
}
}
}
])
解決方案 1 :如果您只想對數據進行分組。
db.items.aggregate([
{ $unwind: "$marketData.history" },
{
$group: {
_id: {
category: "$category",
timestamp: "$marketData.history.timestamp"
},
price: { $sum: "$marketData.history.price" },
volume: { $sum: "$marketData.history.volume" }
}
},
{
$project: {
_id: 0,
category: "$_id.category",
timestamp: "$_id.timestamp",
price: "$price",
volume: "$volume"
}
},
{
$group: {
_id: null,
result: { $push: "$$ROOT" }
}
}
]);
解決方案 2 :與您預期的 output 完全匹配。
db.items.aggregate([
{ $unwind: "$marketData.history" },
{
$group: {
_id: {
category: "$category",
timestamp: "$marketData.history.timestamp"
},
name: { $first: "$name" },
currency: { $first: "$marketData.currency" },
price: { $sum: "$marketData.history.price" },
volume: { $sum: "$marketData.history.volume" }
}
},
{
$sort: { "_id.timestamp": 1 }
},
{
$group: {
_id: "$_id.category",
category: { $first: "$_id.category" },
name: { $first: "$name" },
currency: { $first: "$currency" },
history: {
$push: {
timestamp: "$_id.timestamp",
price: "$price",
volume: "$volume"
}
}
}
},
{
$addFields: {
marketData: {
currency: "$currency",
history: "$history"
},
currency: "$$REMOVE",
history: "$$REMOVE"
}
}
]);
Output:
/* 1 */
{
"_id" : "My Category",
"category" : "My Category",
"name" : "My Name",
"marketData" : {
"currency" : "€",
"history" : [
{
"timestamp" : 1,
"price" : 10,
"volume" : 20
},
{
"timestamp" : 3,
"price" : 2.2,
"volume" : 20
}
]
}
},
/* 2 */
{
"_id" : "My Category 2",
"category" : "My Category 2",
"name" : "My Name 2",
"marketData" : {
"currency" : "€",
"history" : [
{
"timestamp" : 1,
"price" : 1.1,
"volume" : 10
},
{
"timestamp" : 3,
"price" : 2.2,
"volume" : 20
}
]
}
}
此外,您也會收到該錯誤,因為有一個額外的結束}
大括號,並且marketData.history
沒有包含為"marketData.history"
。 檢查以下更正后的查詢:
db.items.aggregate([
{ $unwind: "$marketData.history" },
{
$group: {
_id: "$marketData.history.timestamp",
price: { $sum: "$marketData.history.price" }
}
},
{
$group: {
_id: null,
price: {
$push: { timestamp: "$_id", price: "$price" }
}
}
},
{
$project: { _id: 0 }
}
])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.