Object (Document) (Nested Document)

Last updated: 2026-10-09

Object (Document) (Nested Document)

Object is a nested subdocument. MongoDB nests arbitrarily deep for self-describing models. Access nested fields via dot paths, quoted in queries, e.g. `'profile.address.city'`. Ideal for one-to-one data (user profiles, order addresses); promote separately queried or growing data to its own collection.

CategoryBSON Types
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ profile: { name: "alice", address: { city: "beijing" } } }
db.users.find({ "profile.address.city": "beijing" })

Returns:The nested document itself; queries return matching documents

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// Object: write and query a nested document
use shop
db.users.insertOne({ profile: { name: 'alice', address: { city: 'beijing' } } })
db.users.find({ 'profile.address.city': 'beijing' })

Output:

{ _id: ObjectId('...'), profile: { name: 'alice', address: { city: 'beijing' } } }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QHow deep can documents nest?
AThe official nesting limit is about 100 levels, practically constrained by the 16MB document size first.
QEmbed or use a separate collection?
AEmbed one-to-one data read with the parent; separate collections for independently growing or queried data.
QHow do I query nested fields?
AUse a quoted dot path: { 'profile.address.city': 'beijing' }.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏