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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gabriel
  • 65
  • 2
  • 9
  • What error do you get? – TechWisdom Feb 26 '19 at 23:10
  • Thank you TechWisdom. I'm not getting any error, simply no results are found: – Gabriel Feb 26 '19 at 23:13
  • Fetched 0 record(s) in 1837ms <- This is from Robo 3T 1.2 – Gabriel Feb 26 '19 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 Feb 26 '19 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 Feb 26 '19 at 23:21
  • Try to query this: `db.getCollection('EventDateValidation').find({"validationStepData.2.deadlineDate.auditedDate":{$exists:true}})`. What happens? (Nothing to do with the curly brackets, it's the same) – TechWisdom Feb 26 '19 at 23:22
  • I just tried the query you sent me in both Robo 3T and Studio 3T and I didn't get any results in any of them. => Fetched 0 record(s) in 176ms. Is there any way to put screenshots in my question so I can show you in detail? – Gabriel Feb 26 '19 at 23:29
  • Yes, you can add screenshots in the text editor – TechWisdom Feb 26 '19 at 23:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189099/discussion-between-gabriel-and-techwisdom). – Gabriel Feb 26 '19 at 23:45

2 Answers2

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}})
TechWisdom
  • 3,960
  • 4
  • 33
  • 40
  • 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. – Gabriel Feb 27 '19 at 16:14
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} }}})
Austin Yi
  • 61
  • 3
  • Interestingly dot notation should work either, even with an array. – TechWisdom Feb 27 '19 at 00:07
  • 1
    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. – Gabriel Feb 27 '19 at 16:15