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})

返回匹配的文档 (Document 1),但总是包含 shapes 中的所有数组项:

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

但是,我想仅使用包含 color=red 的数组来获取文档 (Document 1):

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

我该怎么做?

最佳答案

MongoDB 2.2 的新 $elemMatch投影运算符提供了另一种方法来更改返回的文档以仅包含 first 匹配的 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"
            }
        ]
    }
]

https://stackoverflow.com/questions/3985214/

相关文章:

javascript - 如何在 Mongoose 中更新/插入文档?

mongodb - 获取集合中所有键的名称

mongodb - NoSQL (MongoDB) vs Lucene (或 Solr) 作为你的数

mongodb - 使用另一个字段的值更新 MongoDB 字段

javascript - 如何使用 Node.js Mongoose 删除文档?

mongodb - 如何导出 MongoDB 中的所有集合?

mongodb - 如何从 MongoDB 获取随机记录?

mongodb - 向 MongoDB 集合中的每个文档添加新字段

mongodb - 在 MongoDB 中实现数据版本控制的方法

mongodb - 如何在 Mongo 中查询 "is not null"?