Mongodb 如何在Mongo DB中检索数组中存在的元素?

Mongodb 如何在Mongo DB中检索数组中存在的元素?,mongodb,mongodb-query,mongodb-java,spring-data-mongodb,Mongodb,Mongodb Query,Mongodb Java,Spring Data Mongodb,我的结构如下所示: { field1: "somevalue", name:"xtz", nested_documents: [ { x:1, y:2, info:[ {name:"sachi

我的结构如下所示:

{
  field1: "somevalue",
  name:"xtz",
  nested_documents: [ 
                        {   
                            x:1,
                            y:2,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        },
                        {
                            x:1,
                            y:3,
                            info:[
                                    {name:"sachin1",value:"test"},
                                    {name:"sachin2", value:"test"}
                                 ]
                        },
                        {
                            x:4,
                            y:3,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        }
                    ]
    }
db.test.find({"nested_documents.info.name": "sachin"}, 
        {_id: 0, 'nested_documents.info': {$elemMatch: {name: "sachin"}}});
db.test.insert( {
  field1: "somevalue",
  name:"xtz",
  nested_documents: [ 
                        {   
                            x:1,
                            y:2,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        },
                        {
                            x:1,
                            y:3,
                            info:[
                                    {name:"sachin1",value:"test"},
                                    {name:"sachin2", value:"test"}
                                 ]
                        },
                        {
                            x:4,
                            y:3,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        }
                    ]
    }
    )
我知道我可以使用以下代码检索第一个数组中的元素:

db.test.find({"nested_documents.x": 1},{_id: 0, nested_documents: {$elemMatch: {x: 1}}}
但是,我想对
name
属性应用相同的逻辑。 我只想检索名为“sachin”的文档。 我所做的尝试如下所示:

{
  field1: "somevalue",
  name:"xtz",
  nested_documents: [ 
                        {   
                            x:1,
                            y:2,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        },
                        {
                            x:1,
                            y:3,
                            info:[
                                    {name:"sachin1",value:"test"},
                                    {name:"sachin2", value:"test"}
                                 ]
                        },
                        {
                            x:4,
                            y:3,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        }
                    ]
    }
db.test.find({"nested_documents.info.name": "sachin"}, 
        {_id: 0, 'nested_documents.info': {$elemMatch: {name: "sachin"}}});
db.test.insert( {
  field1: "somevalue",
  name:"xtz",
  nested_documents: [ 
                        {   
                            x:1,
                            y:2,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        },
                        {
                            x:1,
                            y:3,
                            info:[
                                    {name:"sachin1",value:"test"},
                                    {name:"sachin2", value:"test"}
                                 ]
                        },
                        {
                            x:4,
                            y:3,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        }
                    ]
    }
    )
但是Mongo db说它不支持“.”操作符内部投影:(.
还有其他方法吗?(使用命令提示符或代码)

插入文档的命令如下所示:

{
  field1: "somevalue",
  name:"xtz",
  nested_documents: [ 
                        {   
                            x:1,
                            y:2,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        },
                        {
                            x:1,
                            y:3,
                            info:[
                                    {name:"sachin1",value:"test"},
                                    {name:"sachin2", value:"test"}
                                 ]
                        },
                        {
                            x:4,
                            y:3,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        }
                    ]
    }
db.test.find({"nested_documents.info.name": "sachin"}, 
        {_id: 0, 'nested_documents.info': {$elemMatch: {name: "sachin"}}});
db.test.insert( {
  field1: "somevalue",
  name:"xtz",
  nested_documents: [ 
                        {   
                            x:1,
                            y:2,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        },
                        {
                            x:1,
                            y:3,
                            info:[
                                    {name:"sachin1",value:"test"},
                                    {name:"sachin2", value:"test"}
                                 ]
                        },
                        {
                            x:4,
                            y:3,
                            info:[
                                    {name:"sachin",value:"test"},
                                    {name:"sachin", value:"test"}
                                 ]
                        }
                    ]
    }
    )
我希望输出为:

{ "_id" : ObjectId("5142e0f153cd2aab3a3bae5b"), 
"nested_documents" : [ 
                        {       "x" : 1,        "y" : 2,       
                        "info" : [     
                                    {       "name" : "sachin",      "value" : "test" },   
                                    {       "name" : "sachin",      "value" : "test" } 
                                ] 
                        },
                        {      "x" : 4,        "y" : 3,        
                        "info" : [      {       "name" : "sachin",      "value" : "test" },
                                        {       "name" : "sachin",      "value" : "test" } 
                                ] 
                        } 
                    ]
}

您必须将aggregate()与双$unwind一起使用,如下所示:

db.test.aggregate([
    // filter for documents with x=1
    // note: this will use an index, if defined on "nested_documents.x"
    //{ $match: { "nested_documents.x": 1 } },
    // reduce data to nested_documents, as other fields are not considered
    { $project: { nested_documents: 1 } },
    // flatten the outer array
    { $unwind: "$nested_documents" },
    // filter for nested_documents with x=1
    // note that at this point nested_documents is no longer an array
    //{ $match: { "nested_documents.x": 1 } },
    // flatten the inner array
    { $unwind: "$nested_documents.info" },
    // filter for nested_documents.info.name = "sachin"
    // note that at this point nested_documents.info is no longer an array
    { $match: { "nested_documents.info.name": "sachin" } },
    // format output: re-create inner array
    { $group: { _id: { id: "$_id", 
                       nested_documents: { 
                          x: "$nested_documents.x",
                          y: "$nested_documents.y" 
                       }
                     }, 
                     info: { $push: "$nested_documents.info" } } },
    { $project: { "nested_documents.x": "$_id.nested_documents.x",
                  "nested_documents.y": "$_id.nested_documents.y",
                  "nested_documents.info": "$info" } },
    // format output: re-create outer array
    { $group: { _id: "$_id.id", nested_documents: { $push: "$nested_documents" } } },
])
注意:我在注释中加入了过滤x=1的逻辑,正如您在前面的示例中所做的那样

结果是:

{
    "result" : [
        {
            "_id" : ObjectId("515d873457a0887a97cc8d19"),
            "nested_documents" : [
                {
                    "x" : 4,
                    "y" : 3,
                    "info" : [
                        {
                            "name" : "sachin",
                            "value" : "test"
                        },
                        {
                            "name" : "sachin",
                            "value" : "test"
                        }
                    ]
                },
                {
                    "x" : 1,
                    "y" : 2,
                    "info" : [
                        {
                            "name" : "sachin",
                            "value" : "test"
                        },
                        {
                            "name" : "sachin",
                            "value" : "test"
                        }
                    ]
                }
            ]
        }
    ],
    "ok" : 1
}

有关聚合的详细信息,请参阅

,如果您编辑了示例文档,以便可以将其直接粘贴到插入中,则会有所帮助。@JohnnyHK:我已更新了代码。这只是您另一个问题的另一种风格。请参阅我在该问题上链接的副本,该问题涉及如何使用聚合框架。@JohnnyHK:T他的问题与检索子文档中存在的子文档有关。另一个问题与检索所有匹配的文档有关,如“db.test.find({“nested_documents.x”:1},{u id:0,nested_documents:{$elemMatch:{x:1}}”不检索所有文档。在这个问题中,不允许在投影中嵌套,所以我想问是否有任何解决方法。