-5

如何从文档中获取子数组?

这是其中一份文件:

"director_details": {
    "0": {
        "AppointmentDate": "2010-06-02",
        "AppointmentStatus": "CURRENT",
        "AppointmentType": "SEC",
        "NumAppointments": "1",
        "Person": {
            "CountryOfResidence": [],
            "DOB": [],
            "Forename": "MARK JHONSONE",
            "Nationality": "NATIONALITY UNKNOWN",
            "PersonAddress": {
                "AddressLine": "5 PARSONS STREET",
                "Country": [],
                "County": "WEST MIDLANDS",
                "PostTown": "DUDLEY",
                "Postcode": "DY1 1JJ"
            },
            "PersonID": "CSeJxNkEELgkAQhe/9CvFerlpZsK0IFUSEUFZH2dyxlnK1XY3897mG5WV48/HezDDYf2cP4wVS8VwsTHuETANEkjMurgvzGK2HM9PwyQDfoW4qLQpV0rJSBGHr32he1gUQu6WtxIzLNJcgaAZkF+y3 [...]",
            "Surname": "WESTWOOD",
            "Title": "MR"
        }
    },
    "1": {
        "AppointmentDate": "2010-06-02",
        "AppointmentStatus": "CURRENT",
        "AppointmentType": "DIR",
        "NumAppointments": "1",
        "Occupation": "DIRECTOR",
        "Person": {
            "CountryOfResidence": "UNITED KINGDOM",
            "DOB": "1979-11-30",
            "Forename": "MARK DAVID",
            "Nationality": "BRITISH",
            "PersonAddress": {
                "AddressLine": "5 PARSONS STREET",
                "Country": [],
                "County": "WEST MIDLANDS",
                "PostTown": "DUDLEY",
                "Postcode": "DY1 1JJ"
            },
            "PersonID": "CSeJxNkN0KgkAQhe97CvG+XCt/gm0jqCAihLK6lE3HWsp129XIt881TG+GMx9nZg6DF5/sabxBKpbzuWmPkGkAj/OE8dvcPIWboW8aCzLAD6jqSoVQBS1KRRC2ukbzohJAxg1tJE6YTHMJnGZA9svD [...]",
            "Surname": "WESTWOOD",
            "Title": "MR"
        }
    }
}

我想搜索名字包含“大卫”并且姓氏和头衔不为空的导演。

我有一个运行良好的查询,但它也返回了我想从输出中删除的额外子数组。

4

1 回答 1

3

好的。假设您的文档是:

{
    "director_details": {
        "0": {
            "AppointmentDate": "2010-06-02",
            ...
        },
        "1": {
            "AppointmentDate": "2010-06-02",
            ...
            }
        }
    }
}

首先,director_details在你的问题不是一个数组,它是一个对象。并且由于以下几个原因director_details.Person.Forename: /david/ 不匹配

  1. 对象图的一部分丢失,路径Forenamedirector_details.1.Person.Forename
  2. MongoDB 中的正则表达式默认区分大小写,您应该切换i标志以使其不区分大小写

将匹配的正确查询是:db.directors.find({"director_details.1.Person.Forename" : /DAVID/})。使用当前的数据模型,它根本不灵活,因为您需要指定0or 1

考虑制作director_details一个数组,例如:

{
    "director_details": [
        {
            "AppointmentDate": "2010-06-02",
            ...
        },
        {
            "AppointmentDate": "2010-06-02",
            ...
        }
    ]
}

在这种情况下,查询非常简单

db.directors.find({}, {"director_details" : {$elemMatch : {"Person.Forename" : /DAVID/}}})

这是运行它的结果:

> db.directors.find({}, {"director_details" : {$elemMatch : {"Person.Forename" : /DAVID/}}}).pretty()
{
        "_id" : ObjectId("52455db9cafed39bf0dee631"),
        "director_details" : [
                {
                        "AppointmentDate" : "2010-06-02",
                        "AppointmentStatus" : "CURRENT",
                        "AppointmentType" : "DIR",
                        "NumAppointments" : "1",
                        "Occupation" : "DIRECTOR",
                        "Person" : {
                                "CountryOfResidence" : "UNITED KINGDOM",
                                "DOB" : "1979-11-30",
                                "Forename" : "MARK DAVID",
                                "Nationality" : "BRITISH",
                                "PersonAddress" : {
                                        "AddressLine" : "5 PARSONS STREET",
                                        "Country" : [ ],
                                        "County" : "WEST MIDLANDS",
                                        "PostTown" : "DUDLEY",
                                        "Postcode" : "DY1 1JJ"
                                },
                                "PersonID" : "CSeJxNkN0KgkAQhe97CvG+XCt/gm0jqCAihLK6lE3HWsp129XI",
                                "Surname" : "WESTWOOD",
                                "Title" : "MR"
                        }
                }
        ]
}
于 2013-09-27T10:33:08.953 回答