0
0
MongoDBquery~5 mins

Dot notation for embedded documents in MongoDB

Choose your learning style9 modes available
Introduction

Dot notation helps you look inside nested parts of your data easily.

You want to find a specific detail inside a nested object in your database.
You need to update a small part inside a bigger document without changing everything.
You want to sort or filter data based on a value inside an embedded document.
You want to show only a small piece of nested data when you get results.
Syntax
MongoDB
db.collection.find({"outer.inner": value})

Use quotes around the field path if it contains special characters.

Dot notation uses a dot (.) to go deeper inside nested documents.

Examples
Find users who live in the city named New York inside their address.
MongoDB
db.users.find({"address.city": "New York"})
Update the phone number of the customer named Alice inside the orders collection.
MongoDB
db.orders.updateOne({"customer.name": "Alice"}, {$set: {"customer.phone": "123-456"}})
Show only the weight inside the details of each product, hiding the _id.
MongoDB
db.products.find({}, {"details.weight": 1, _id: 0})
Sample Program

This query finds all library documents where the book's author's name is John Doe.

MongoDB
db.library.find({"book.author.name": "John Doe"})
OutputSuccess
Important Notes

Dot notation only works with embedded documents, not arrays directly.

If you want to query inside arrays, use special operators like $elemMatch.

Summary

Dot notation lets you access nested fields inside documents easily.

Use it to find, update, or project parts of embedded documents.

Remember to use quotes if the field names have special characters.