Grokking MongoDB
0% completed
Querying Embedded/Nested Documents
These two filters look alike, yet on the same data the first returns two users and the second returns none.
db.users.find({ "address.city": "Anytown" }, { name: 1, _id: 0 }) db.users.find({ address: { city: "Anytown" } }, { name: 1, _id: 0 })
[ { name: 'John Doe' }, { name: 'Alice' } ] []
The difference lies in how each filter reads the address field. In the documents below, address holds a smaller document inside the main one, which is known as an embedded or nested document
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%