I have have a collection called users. Each user document has an embedded document called phone numbers.
}
"_id": ObjectId("4f2984b1af06e80418000000"),
"User_FirstName": "Frank",
"User_LastName": "Williams",
"User_PhoneNumbers": [
{"Phone_Type": "Home","Phone_Number: "555-555-5555" },
{"Phone_Type": "Mobile","Phone_Number: "333-333-3333" }
]
}
I am trying to display all the phone numbers for the user named “Frank Williams”. I would like to use a loop that returns the results (all phone numbers for a particular user):
Home, 555-555-5555
Mobile, 333-333-33333
I am new to mongodb and don’t know how to query an embedded document and have it return all the embedded documents for that particular user. I am coding in php. Thank you for any help.
At best you can return only the whole
User_PhoneNumbersarray. You cannot pick individual items out of it. (I am talking about regular queries, of course. With map-reduce or upcoming Aggregation Framework you’ll be able to do this).This query will return this document:
Then in PHP you can handle it however you want.