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.
| Category | BSON Types |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (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
- Dot paths access nested fields; escape keys containing dots or $
- Embed for one-to-one; arrays or separate collections for one-to-many
- $set replaces whole subdocuments; dot paths update single fields
🏷️ Related Tags
💡 Related Cases
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' }.