Introduction
Sometimes data is stored inside other data. We need to find information deep inside these layers.
Jump into concepts and practice - no test required
Sometimes data is stored inside other data. We need to find information deep inside these layers.
{ 'outer.inner.deep': value }Use dots (.) to go inside nested fields.
Replace 'outer.inner.deep' with your actual nested field names.
{ 'address.city': 'New York' }{ 'product.details.price': 100 }{ 'comments.text': 'great' }This finds users whose email inside profile > contact is 'alice@example.com'.
db.users.find({ 'profile.contact.email': 'alice@example.com' })If the nested field does not exist, the query will not match that document.
You can use this dot notation with other query operators like $gt, $lt, $in.
Use dot notation to reach inside nested fields.
This helps find data stored deep inside documents.
It works with any depth of nesting.
address.city inside a document?profile.details.age equals 30?{ "user": { "contact": { "email": "a@example.com" } } }, { "user": { "contact": { "email": "b@example.com" } } }, { "user": { "contact": { "phone": "12345" } } }db.collection.find({ "user.contact.email": { $exists: true } }) return?profile.address.zipcode is "12345":db.users.find({ profile.address.zipcode: "12345" })settings.preferences.notifications.email.enabled. How would you write a MongoDB query to find all documents where email notifications are enabled (true), regardless of nesting depth?