Suppose I have a database of Patients, each of which might have more than one physical address, so the database has an Array of Addresses. I want to find a certain Patient and return only a certain field from each address in the array of Addresses.
If I use this, I successfully get every address (array member) and every detail (street, number, city, zip, etc.) of each address:
db.Patients.find( { "LastName": "Smith" }, { "Addresses": 1 } )
I try this, hoping to get only the City of the first address
db.Patients.find( { "LastName": "Smith" }, { "Addresses.0.City": 1 } )
or I try this (to simplify things), hoping to get all the details of (only) the first address
db.Patients.find( { "LastName": "Smith" }, { "Addresses.0": 1 } )
Instead, in both of those cases, I get the Patient record with an empty document for Addresses. But I don't understand why?
Thanks!
db.Patients.find( { "LastName": "Smith" }, { "Addresses.City": 1 } )?{ "Addresses": { "Home": { "street": "...", "city": "..." }, "Work": { ... } } }as I suspect you actually have, is not an array. Which explains why the notation you are using in your projection returns nothing.