MongoDB-查询嵌套数组中的嵌套对象

时间:2019-02-26 22:59:01

标签: javascript mongodb mongodb-query embedded-documents

我正在为您需要的查询请求您的帮助,但我对MongoDB的了解还不是很熟练。我的数据结构如下:

db.getCollection('EventDateValidation').find({}):

/* 1 */
{
    "_id" : ObjectId("5b7b2e3ae5e2100007717d81"),
    "_class" : "com.overwatch.common.model.EventDateValidation",
    "caseNo" : "OW000002269122201810201135",
    "loanNo" : "000002269122",
    "eventType" : "BREACLETTR",
    "validationStepData" : [ 
        {
            "startDate" : {
                "isChecked" : "Y",               
                "comments" : "",
                "auditedBy" : "Mahalakshmi M",
                "auditedDate" : "2018-12-12"
            }
        }, 
        {
            "completedDate" : {
                "isChecked" : "Y",
                "comments" : "",
                "auditedBy" : "Mahalakshmi M",
                "auditedDate" : "2018-12-13"
            }
        }, 
        {
            "deadlineDate" : {
                "isChecked" : "Y",
                "comments" : "",
                "auditedBy" : "Mahalakshmi M",
                "auditedDate" : "2018-12-13"
            }
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5b7c11095c2b4d0007bc8c54"),
    "_class" : "com.overwatch.common.model.EventDateValidation",
    "caseNo" : "OW000000854076201808181158",
    "loanNo" : "000000854076",
    "eventType" : "FORSALAPPR",
    "validationStepData" : [ 
        {
            "startDate" : {
                "comments" : ""
            }
        }, 
        {
            "completedDate" : {
                "comments" : "Received Date = 8/4/2017"
            }
        }, 
        {
            "deadlineDate" : {
                "comments" : ""
            }
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("5b7ad05d5c2b4d0007bc8631"),
    "_class" : "com.overwatch.common.model.EventDateValidation",
    "caseNo" : "OW000000873954201810201235",
    "loanNo" : "000000873954",
    "eventType" : "HUDNOTIFCA",
    "validationStepData" : [ 
        {
            "startDate" : {
                "isChecked" : "Y",
                "comments" : "",
                "auditedBy" : "Brett Scott",
                "auditedDate" : "2018-09-25"
            }
        }, 
        {
            "completedDate" : {
                "isChecked" : "Y",
                "comments" : "",
                "auditedBy" : "Brett Scott",
                "auditedDate" : "2018-09-25"
            }
        }, 
        {
            "deadlineDate" : {
                "isChecked" : "Y",
                "comments" : "",
                "auditedBy" : "Brett Scott",
                "auditedDate" : "2018-09-25"
            }
        }
    ]
}

从此集合中,我需要在“ deadlineDate”中找到具有“ auditedDate”的文档。在此示例中,我将找到文档1和3。请牢牢抓住我。

我尝试过

db.getCollection('EventDateValidation').find({"validationStepData.deadlineDate.auditedDate":{$exists:true}})

但似乎不起作用。请帮忙!

2 个答案:

答案 0 :(得分:1)

点表示法不起作用,因为您在validationStepData中有一系列对象。您可以使用$elemMatch将查询条件应用于与表达式匹配的数组元素。

db.getCollection('EventDateValidation').find({"validationStepData" : { $elemMatch: {"deadlineDate.auditedDate" : {$exists:true} }}})

答案 1 :(得分:1)

只是为了解决问题:问题中的查询效果很好。我与@Gabriel聊天,问题是Robomongo向查询添加了隐藏的不可打印的Unicode字符。

总而言之,对于任何感兴趣的游牧民族,以下是查询对象数组的几种方法:

1)隐式$ elemMatch /数组上的简单点表示法:

#include <iostream>
#include <string>

using namespace std;

int numItems;

void userInput(string *terms, string *def) {

    for (int i = 0; i < numItems; ++i) {
        cout << "Enter Term: ";
        cin >> terms[i];
        cout << endl;

        cout << "Enter Def for that term: ";
        cin >> def[i];
        cout << endl;

    }
}


void outputdef() {

}

int main() {

    cout << "How many Items do you want: ";
    cin >> numItems;
    cout << endl;

    string *ptrTerms = new string[numItems];
    string *ptrDef = new string[numItems];

    userInput(ptrTerms, ptrDef);

    delete ptrTerms;
    delete ptrDef;

}

2)显式$ elemMatch(我们可以有多个查询条件):

db.getCollection('EventDateValidation').find({"validationStepData.deadlineDate.auditedDate": {$exists:true}})

3)具有索引位置的数组点表示法(当我们知道元素在数组内的确切位置时):

db.getCollection('EventDateValidation').find({"validationStepData": { $elemMatch: {"deadlineDate.auditedDate" : {$exists:true} }}})