如何从mongo文件中获取子数据?

时间:2013-09-26 19:20:42

标签: mongodb sub-array

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

以下是其中一份文件:

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

我想搜索导演哪个名字包含“david”和姓氏&标题不是空的。

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

1 个答案:

答案 0 :(得分:3)

确定。假设您的文档是:

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

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

  1. 错过了对象图表的一部分,Forename的路径为director_details.1.Person.Forename
  2. 默认情况下,MongoDB中的正则表达区分大小写,you should toggle i flag to make it case-insensitive
  3. 匹配的正确查询是:db.directors.find({"director_details.1.Person.Forename" : /DAVID/})。使用当前数据模型时,它根本不灵活,因为您需要指定01

    考虑将director_details作为数组,例如:

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

    在这种情况下,查询is very simple

    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"
                            }
                    }
            ]
    }