MongoDBコレクションのオブジェクト配列内のクエリされた要素のみを取得する 質問する

MongoDBコレクションのオブジェクト配列内のクエリされた要素のみを取得する 質問する

私のコレクションに次の文書があるとします。

{  
   "_id":ObjectId("562e7c594c12942f08fe4192"),
   "shapes":[  
      {  
         "shape":"square",
         "color":"blue"
      },
      {  
         "shape":"circle",
         "color":"red"
      }
   ]
},
{  
   "_id":ObjectId("562e7c594c12942f08fe4193"),
   "shapes":[  
      {  
         "shape":"square",
         "color":"black"
      },
      {  
         "shape":"circle",
         "color":"green"
      }
   ]
}

クエリを実行:

db.test.find({"shapes.color": "red"}, {"shapes.color": 1})

または

db.test.find({shapes: {"$elemMatch": {color: "red"}}}, {"shapes.color": 1})

一致したドキュメント(ドキュメント 1)を返しますが、常にすべての配列項目が含まれますshapes

{ "shapes": 
  [
    {"shape": "square", "color": "blue"},
    {"shape": "circle", "color": "red"}
  ] 
}

ただし、次の内容を含む配列のみを使用してドキュメント(ドキュメント 1)を取得したいと思いますcolor=red

{ "shapes": 
  [
    {"shape": "circle", "color": "red"}
  ] 
}

これどうやってするの?

ベストアンサー1

MongoDB 2.2の新機能$elemMatch射影演算子は、返されたドキュメントを変更して、最初に一致したshapes要素のみを含むようにする別の方法を提供します。

db.test.find(
    {"shapes.color": "red"}, 
    {_id: 0, shapes: {$elemMatch: {color: "red"}}});

戻り値:

{"shapes" : [{"shape": "circle", "color": "red"}]}

2.2では、$ projection operatorここで、$投影オブジェクトのフィールド名の は、クエリからのフィールドの最初の一致する配列要素のインデックスを表します。次のコードは、上記と同じ結果を返します。

db.test.find({"shapes.color": "red"}, {_id: 0, 'shapes.$': 1});

MongoDB 3.2 アップデート

3.2リリース以降では、新しい$filter投影中に配列をフィルター処理する集計演算子。これには、最初の一致だけでなくすべての一致を含めるという利点があります。

db.test.aggregate([
    // Get just the docs that contain a shapes element where color is 'red'
    {$match: {'shapes.color': 'red'}},
    {$project: {
        shapes: {$filter: {
            input: '$shapes',
            as: 'shape',
            cond: {$eq: ['$$shape.color', 'red']}
        }},
        _id: 0
    }}
])

結果:

[ 
    {
        "shapes" : [ 
            {
                "shape" : "circle",
                "color" : "red"
            }
        ]
    }
]

おすすめ記事