最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongoDB - Query nested objects in nested array - Stack Overflow

programmeradmin2浏览0评论

I'm kindly requesting your help for this query that I need to do and I'm not very proficient yet in MongoDB. My data structure looks like this:

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

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

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

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

From this collection, I need to find the documents that have an "auditedDate" in the "deadlineDate". In this example, I would find the documents 1 and 3. Please help me as I'm stuck on this one.

I have tried

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

But doesn't seem to work. Help please!

I'm kindly requesting your help for this query that I need to do and I'm not very proficient yet in MongoDB. My data structure looks like this:

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

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

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

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

From this collection, I need to find the documents that have an "auditedDate" in the "deadlineDate". In this example, I would find the documents 1 and 3. Please help me as I'm stuck on this one.

I have tried

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

But doesn't seem to work. Help please!

Share Improve this question edited Mar 4, 2019 at 21:57 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 26, 2019 at 22:59 GabrielGabriel 652 silver badges11 bronze badges 9
  • What error do you get? – TechWisdom Commented Feb 26, 2019 at 23:10
  • Thank you TechWisdom. I'm not getting any error, simply no results are found: – Gabriel Commented Feb 26, 2019 at 23:13
  • Fetched 0 record(s) in 1837ms <- This is from Robo 3T 1.2 – Gabriel Commented Feb 26, 2019 at 23:14
  • I've made a collection just like yours and it worked for me. What do you get when you query: db.getCollection('EventDateValidation').find()? – TechWisdom Commented Feb 26, 2019 at 23:18
  • Thanks, when I execute db.getCollection('EventDateValidation').find({}), it will display all those three documents. Does it have to do with the curly brackets inside find()? – Gabriel Commented Feb 26, 2019 at 23:21
 |  Show 4 more ments

2 Answers 2

Reset to default 3

Just for clearing things up: the query in the question works well. I chatted with @Gabriel, and the problem was that Robomongo added hidden non-printable unicode characters to the query.

All in all, for any interested nomads, here are few ways to query an array of objects:

1) Implicit $elemMatch / simple dot notation on an array:

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

2) Explicit $elemMatch (we can have multiple query criteria):

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

3) Array dot notation with an index position (when we know the exact position of an element inside an array):

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

Dot notation wouldn't work since you have an array of objects within validationStepData. You could use $elemMatch to apply your query conditions to the array elements that match your expression.

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

评论列表(0)

  1. 暂无评论