3

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

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!

9
  • What error do you get? Commented Feb 26, 2019 at 23:10
  • Thank you TechWisdom. I'm not getting any error, simply no results are found: Commented Feb 26, 2019 at 23:13
  • Fetched 0 record(s) in 1837ms <- This is from Robo 3T 1.2 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()? 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()? Commented Feb 26, 2019 at 23:21

2 Answers 2

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}})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much TechWisdom, I appreciate your help and the discussion we had on the chat. I have tried the three ways you mention in your query and only the second one (with $elemMatch) is actually working with the expected results. That's what I needed, thanks a lot.
1

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

2 Comments

Interestingly dot notation should work either, even with an array.
Thank you Austin, you're right, the dot notation is not working for some reason. The approach that actually worked for me is the use of $elemMatch, as you mention. This way it's showing the expected results. I appreciate your input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.