Skip to main content

Find Query on Nested Documents

Populate inventory collection with the data to start using find query.

note

Delete inventory collection and populate new data.

db.inventory.drop()
db.inventory.insertMany([
{ item: "Learn MongoDB", qty: 25, size: { h: 20, w: 8 }, publishDate: '2022-10-01' },
{ item: "MongoDB for Dummies", qty: 25, size: { h: 10, w: 10 }, publishDate: '2018-11-17' },
{ item: "MongoDB Handbook", qty: 25, size: { h: 10, w: 10 }, publishDate: '2019-10-01' },
{ item: "Professional MongoDB", qty: 25, size: { h: 10, w: 10 }, publishDate: '2022-10-01' },
{ item: "MongoDB Performance", qty: 25, size: { h: 10, w: 10 }, publishDate: '2021-10-01' },
{ item: "Beginning MongoDB", qty: 25, size: { h: 10, w: 10 }, publishDate: '2022-10-01' },
{ item: "MongoDB Administration", qty: 25, size: { h: 10, w: 10 }, publishDate: '2022-09-01' }
]);

Find records based on nested conditions

Get records from inventory where the w and h of size are, 20 and 8 respectively.

const cursor = db.collection('inventory').find({
size: { w: 20, h: 8 },
});