mongodb多密钥聚合
我有sql背景,了解mongodb中聚合数据的语法,但在mongodb中“展平”多键聚合的输出时遇到了麻烦。例如,标准语法如下所示:mongodb多密钥聚合,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有sql背景,了解mongodb中聚合数据的语法,但在mongodb中“展平”多键聚合的输出时遇到了麻烦。例如,标准语法如下所示: db.transactions.aggregate( [ { $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$total" } } } ] ); 但这会以以下形式返回数据: [{"_id":{"category":"F
db.transactions.aggregate(
[
{ $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$total" } } }
]
);
但这会以以下形式返回数据:
[{"_id":{"category":"Fees","postdate":"2013-01-04T05:00:00.000Z"},"total":24},
{"_id":{"category":"Fees","postdate":"2012-12-20T05:00:00.000Z"},"total":-0.02}]
我想要的是以下格式的数据,其中我仍然在两列或更多列上分组:
备选案文1:
[{"_id":"Auto","postdate":"2013-01-04T05:00:00.000Z","total":24},
{"_id":"Fees","postdate":"2012-12-20T05:00:00.000Z","total":-0.02}]
db.transactions.aggregate(
[
{ $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$total" } } },
{ $project: { "_id":0, "_id":"$_id.category", "postdate":"$_id.postdate", "total":1 } }
]
);
备选案文2:
["category":"Auto","postdate":"2013-01-04T05:00:00.000Z","total":24},
{"category":"Fees","postdate":"2012-12-20T05:00:00.000Z","total":-0.02}]
db.transactions.aggregate(
[
{ $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$total" } } },
{ $project: { "_id":0, "category":"$_id.category", "postdate":"$_id.postdate", "total":1 } }
]
);
如何在mongodb中实现这一点?使用 备选案文1:
[{"_id":"Auto","postdate":"2013-01-04T05:00:00.000Z","total":24},
{"_id":"Fees","postdate":"2012-12-20T05:00:00.000Z","total":-0.02}]
db.transactions.aggregate(
[
{ $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$total" } } },
{ $project: { "_id":0, "_id":"$_id.category", "postdate":"$_id.postdate", "total":1 } }
]
);
备选案文2:
["category":"Auto","postdate":"2013-01-04T05:00:00.000Z","total":24},
{"category":"Fees","postdate":"2012-12-20T05:00:00.000Z","total":-0.02}]
db.transactions.aggregate(
[
{ $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$total" } } },
{ $project: { "_id":0, "category":"$_id.category", "postdate":"$_id.postdate", "total":1 } }
]
);
使用 备选案文1:
[{"_id":"Auto","postdate":"2013-01-04T05:00:00.000Z","total":24},
{"_id":"Fees","postdate":"2012-12-20T05:00:00.000Z","total":-0.02}]
db.transactions.aggregate(
[
{ $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$total" } } },
{ $project: { "_id":0, "_id":"$_id.category", "postdate":"$_id.postdate", "total":1 } }
]
);
备选案文2:
["category":"Auto","postdate":"2013-01-04T05:00:00.000Z","total":24},
{"category":"Fees","postdate":"2012-12-20T05:00:00.000Z","total":-0.02}]
db.transactions.aggregate(
[
{ $group: { _id: {"category":"$category","postdate":"$postdate"} , "total": { $sum: "$total" } } },
{ $project: { "_id":0, "category":"$_id.category", "postdate":"$_id.postdate", "total":1 } }
]
);
选项1第一行应为“费用”。那是打字错误吗?不,第二个应该是“自动”。换了。谢谢,我不明白。您是否只希望类别发生变化?你能提供样品文件吗?您在聚合中按类别分组,后跟发布日期。你还需要一个小组阶段来按类别对他们进行分组。你下面的回答回答了这个问题。那只是个打字错误。我对类别有不同的值。选项1第一行应该是“费用”。那是打字错误吗?不,第二个应该是“自动”。换了。谢谢,我不明白。您是否只希望类别发生变化?你能提供样品文件吗?您在聚合中按类别分组,后跟发布日期。你还需要一个小组阶段来按类别对他们进行分组。你下面的回答回答了这个问题。那只是个打字错误。我对类别有不同的值。