MongoDB查找文档数组中的所有字典

MongoDB查找文档数组中的所有字典,mongodb,Mongodb,我希望在文档中查找与key:value匹配的所有词典。这是两件事 查找名为sample_config222的文档 在我们在1中选择的文档数组中搜索词典 我试过了 { "name": "sample_config222", "data": [ { "id": 1, "first_name": "George", "last_name": "Bluth", }, {

我希望在文档中查找与key:value匹配的所有词典。这是两件事

查找名为sample_config222的文档

在我们在1中选择的文档数组中搜索词典

我试过了

{
    "name": "sample_config222",
    "data": [
        {
            "id": 1,
            "first_name": "George",
            "last_name": "Bluth",
        },
        {
            "id": 2,
            "first_name": "Janet",
            "last_name": "Bluth",
        },
    ]
}
它只显示了第一本词典中的一本

db.config.find({name: "sample_config"}, {"data": { $elemMatch: { "last_name": "Bluth" } } })
此外,它还从包含姓氏:Bluth的其他文档中获取结果

我尝试了$,它显示了整个文档

更新:


有一个类似的问题,使用$elematch只返回数组中的一个字典。但是我想得到所有与查询匹配的dict

您提供的查询

{
    "name": "sample_config222",
    "data": [
        {
            "id": 1,
            "first_name": "George",
            "last_name": "Bluth",
        }
}
分解到这个

查找名称为sample\u config的所有文档 仅项目data.last_name等于Bluth的字段。 这里的关键是find方法需要多个参数

db.config.find({name: "sample_config"}, {"data": { $elemMatch: { "last_name": "Bluth" } } })
可以通过在shell上执行find命令(不带括号)来找到这些命令。这适用于所有shell命令,如果您忘记了如何使用命令,这将非常有用

function (query, fields, limit, skip, batchSize, options)
要获得所需的结果,需要将查询更改为:

db.config.find
注意,我删除了}after sample_config和{before data,使之成为find命令的单个查询文档,而不是查询和项目文档

更新:

我意识到您也希望投影结果文档,因为数组字段只包含匹配的元素。现在我们只需要将原始投影与新查询结合起来

{name: "sample_config", "data": { $elemMatch: { "last_name": "Bluth" } } }
这将返回表单的文档

db.col.find({name: "sample_config", "data": { $elemMatch: { "last_name": "Bluth" } } }, {"data": { $elemMatch: { "last_name": "Bluth" } } })
但是,根据,这将只返回第一个匹配的数组元素,而不是所有匹配的元素

如果需要进一步向下投影数据,我建议使用聚合框架,因为这些操作符更健壮

那可能看起来像

{
    _id: xxx,
    data: [
       { id: xxx, first_name: xxx, last_name: "Bluth" }
    ]
}
这将导致以下输出

db.col2.aggregate({$match: {name: "sample_config", "data": { $elemMatch: { "last_name": "Bluth" } } }},{$unwind:"$data"},{$match:{"data.last_name": "Bluth"}},{$replaceRoot:{newRoot:"$data"}})
从这些原始文件中

{ "id" : 1, "first_name" : "George", "last_name" : "Bluth" }
{ "id" : 2, "first_name" : "Janet", "last_name" : "Bluth" }
{ "id" : 2, "first_name" : "Janet", "last_name" : "Bluth" }
您可以在此处找到这些操作员的文档:


可能是重复的谢谢你的回答。这对我来说不起作用。它正在检索文档数组中的所有dict在更新中,第二个查询工作正常。不确定第一个查询,我只得到一个dict?有没有办法停止限制结果或任何类似的选项?我仍然不完全清楚你在用这些dict做什么数据,但聚合框架将是您最灵活的选择。这对您有用吗?是的,看起来不错。感谢您的帮助:-但第一个问题困扰着我,好像它为什么会导致一个dict。使用elematch时有任何限制。我是Nosql&mongo的新手。不知道其中的查询规划/投影。它是隐藏的$操作符和$elemMatch操作符都根据条件从数组中投影第一个匹配元素。
{ "_id" : ObjectId("5c5b025fea781cb935c975ae"), "name" : "sample_config", "data" : [ { "id" : 1, "first_name" : "George", "last_name" : "Bluth" }, { "id" : 2, "first_name" : "Janet", "last_name" : "Bluth" } ] }
{ "_id" : ObjectId("5c5b0283ea781cb935c975af"), "name" : "sample_config", "data" : [ { "id" : 1, "first_name" : "George", "last_name" : "Dole" }, { "id" : 2, "first_name" : "Janet", "last_name" : "Bluth" } ] }
{ "_id" : ObjectId("5c5b028bea781cb935c975b0"), "name" : "sample_config", "data" : [ { "id" : 1, "first_name" : "George", "last_name" : "Dole" }, { "id" : 2, "first_name" : "Janet", "last_name" : "Johnson" } ] }