0

I have a mongo collection like this :

Collection :rResponse
{
    "_id" : ("59c8248 "),
    "correlationId" : ("b92b3 "),
    "rtResponse" : {
        "rTiming" : {
            "servicetime" : "2020-04-17 11:50:42.1053555 (GMT-04:00)"
}

I Was trying to use find query to access service time.

db.rResponsee.rtResponse.rTiming.find( { servicetime: '2020-05-01 08:25:58.4083775 (GMT-04:00)' } );

But it returns 0 rows. Any help/advice will be greatly appreciated.

Jinja_dude
  • 404
  • 5
  • 20
Bsr
  • 11
  • 5

2 Answers2

0

Your syntax is off, and the entire path to the field needs to appear inside the call to find(...):

db.rResponsee.find({
    'rtResponse.rTiming.servicetime': '2020-05-01 08:25:58.4083775 (GMT-04:00)'
});
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thank you for quick response. It still throw error : Error: Line 1: Unexpected token . – Bsr Feb 26 '21 at 23:46
0
db.rResponse.find({ 'rtResponse.rTiming.servicetime': '2020-04-17 11:50:42.1053555 (GMT-04:00)' }, { 'rtResponse.rTiming.servicetime': 1 })

Output:

{
    "_id" : "59c8248",
    "rtResponse" : {
        "rTiming" : {
            "servicetime" : "2020-04-17 11:50:42.1053555 (GMT-04:00)"
        }
    }
}

Here we are using .find method to query 'servicetime' which is nested document in your case and using dot notation we are querying it, i.e 'rtResponse.rTiming.servicetime'. And we are using projection to specify what fields to include. Consider { 'rtResponse.rTiming.servicetime': 1 } where 1 means true or to include that field.

You could read more about .find method here, and about mongodb projection here.