名前付きキーへの出力は、実際には思っているような素晴らしいものではありません。現実的には、私は通常、返された結果を扱いたいので、 "リスト/配列"はもっと意味があります。
これは、基本的には基本的に彼らの「という名前のキー」のコンセプトを放棄するように言われます、すべての新しい人です、そして、彼らは、データベースと名付けられたキーに固有の問題で作業している実感します。収集が本質的に「リスト」である理由の種類。
ですから、考え方に慣れる方が良いでしょう:基本的にはあなたを与えるために起こっている
db.collection.aggregate([
{ "$unwind": "$arr_vs" },
{ "$group": {
"_id": { "id": "$arr_vs.arr_id", "val": "$arr_vs.val" },
"total_count": { "$sum": 1 }
}},
{ "$group": {
"_id": "$_id.id",
"v": {
"$push": {
"val": "$_id.val",
"total_count": "$total_count"
}
}
}}
])
:
/* 1 */
{
"_id" : "two",
"v" : [
{
"val" : 2.0,
"total_count" : 1.0
},
{
"val" : 5.0,
"total_count" : 1.0
}
]
}
/* 2 */
{
"_id" : "one",
"v" : [
{
"val" : 5.0,
"total_count" : 2.0
}
]
}
/* 3 */
{
"_id" : "three",
"v" : [
{
"val" : 1.0,
"total_count" : 1.0
}
]
}
そして反復可能で使いやすいで集計されたデータであります形。
あなたの出力形式に熱心であり、少なくともMongoDBの3.4.4バージョンを持っている場合は、文書を圧縮し、$arrayToObject
を使用して、そのさらに取ることができます:
db.collection.aggregate([
{ "$unwind": "$arr_vs" },
{ "$group": {
"_id": { "id": "$arr_vs.arr_id", "val": "$arr_vs.val" },
"total_count": { "$sum": 1 }
}},
{ "$group": {
"_id": "$_id.id",
"v": {
"$push": {
"val": "$_id.val",
"total_count": "$total_count"
}
}
}},
{ "$group": {
"_id": null,
"arr_vs": {
"$push": {
"k": "$_id",
"v": "$v"
}
}
}},
{ "$project": {
"_id": 0,
"arr_vs": { "$arrayToObject": "$arr_vs" }
}}
])
あるいは単に「最終の適用
db.collection.aggregate([
{ "$unwind": "$arr_vs" },
{ "$group": {
"_id": { "id": "$arr_vs.arr_id", "val": "$arr_vs.val" },
"total_count": { "$sum": 1 }
}},
{ "$group": {
"_id": "$_id.id",
"v": {
"$push": {
"val": "$_id.val",
"total_count": "$total_count"
}
}
}},
{ "$group": {
"_id": null,
"arr_vs": {
"$push": {
"k": "$_id",
"v": "$v"
}
}
}},
/*
{ "$project": {
"_id": 0,
"arr_vs": { "$arrayToObject": "$arr_vs" }
}}
*/
]).map(d => ({
"arr_vs": d.arr_vs.reduce((acc,curr) =>
Object.assign(acc,({ [curr.k]: curr.v })),{})
}))
そして、両者が同じ出力を生成:
{
"arr_vs" : {
"two" : [
{
"val" : 2.0,
"total_count" : 1.0
},
{
"val" : 5.0,
"total_count" : 1.0
}
],
"one" : [
{
"val" : 5.0,
"total_count" : 2.0
}
],
"three" : [
{
"val" : 1.0,
"total_count" : 1.0
}
]
}
}
0123をお使いのMongoDBのバージョンが新しい演算子をサポートしていない場合は、」クライアント側の形状を変更