Query a document with nested subdocuments

Hello,

I’d like to query a document (using the mongoshell) containing an array of “hotels_booking”. Inside this array, there are several objects containing information about booking. I’d like to retrieve all documents having hotels_booking[n].client.firstName = “limace” ; n being the number of hotels_booking subdocuments.

The attached photo show the field.

I tried to use this query on the mongo shell :
db.inventory.find({ “hotels_booking.client”: { “firstName”: “limace” }}) ; but it does not work.

The way you wrote your query means you want the object hotels_booking.client to be equal to the object {firstName:limace}. The 2 objects are not equals because the hotels_booking.client also has the field lastName.

You have to use the dot notation up to the last field like:

{ "hotels_booking.client.firstName" : "limace" }

Thank you so much for the answer !! It works :slight_smile:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.