Elements Operator
- $exists
// Find products whose field contains price
shop> db.products.find({price: {$exists: true}})
// Find prodcuts whose field contains price and price is greater than 1250
shop> db.products.find({price: {$exists: true}, price: {$gt:1250}})
- $type
// Find products whose isFeatured type is boolean
shop> db.products.find({isFeatured: {$type: 'bool'}})
// Find products whose price is number
shop> db.products.find({price: {$type: 'number'}})
- $size
// Find comments which has 4 comments in the array
shop> db.comments.find({'comments': {$size: 4}})
[
{
_id: 1,
title: 'Introduction to MongoDB',
content: 'MongoDB is a popular NoSQL database...',
author: 'John Doe',
comments: [
{ user: 'Alice', text: 'Great article!' },
{ user: 'Bob', text: 'Thanks for sharing.' },
{ user: 'Eva', text: 'Its beatifull!' },
{ user: 'jessy' }
],
metadata: { views: 1000, likes: 50 }
}
]