Goglides Dev 🌱

Balkrishna Pandey
Balkrishna Pandey

Posted on

Mastering MongoDB Query Operators: Examples and Use Cases

Comparison Operators

Example documents:

{ name: "John", age: 30 }
{ name: "Jane", age: 25 }
{ name: "Bob", age: 40 }
Enter fullscreen mode Exit fullscreen mode

You can use following to create these documents.

db.users.insertMany([{ name: "John", age: 30 },{ name: "Jane", age: 25 },{ name: "Bob", age: 40 }])
Enter fullscreen mode Exit fullscreen mode

Query examples:

  • $eq: find all documents where age is equal to 30
db.users.find({ age: { $eq: 30 } })
Enter fullscreen mode Exit fullscreen mode
  • $ne: find all documents where age is not equal to 30
db.users.find({ age: { $ne: 30 } })
Enter fullscreen mode Exit fullscreen mode
  • $gt: find all documents where age is greater than 30
db.users.find({ age: { $gt: 30 } })
Enter fullscreen mode Exit fullscreen mode
  • $gte: find all documents where age is greater than or equal to 30
db.users.find({ age: { $gte: 30 } })
Enter fullscreen mode Exit fullscreen mode
  • $lt: find all documents where age is less than 30
db.users.find({ age: { $lt: 30 } })
Enter fullscreen mode Exit fullscreen mode
  • $lte: find all documents where age is less than or equal to 30
db.users.find({ age: { $lte: 30 } })
Enter fullscreen mode Exit fullscreen mode

Logical Operators

Example documents:

{ name: "John", age: 30, city: "New York" }
{ name: "Jane", age: 25, city: "Los Angeles" }
{ name: "Bob", age: 40, city: "New York" }
Enter fullscreen mode Exit fullscreen mode

Query examples:

  • $and: find all documents where age is greater than or equal to 30 and city is "New York"
db.users.find({ $and: [{ age: { $gte: 30 } }, { city: "New York" }] })
Enter fullscreen mode Exit fullscreen mode
  • $or: find all documents where age is less than 30 or city is "New York"
db.users.find({ $or: [{ age: { $lt: 30 } }, { city: "New York" }] })
Enter fullscreen mode Exit fullscreen mode
  • $not: find all documents where age is not equal to 30
db.users.find({ age: { $not: { $eq: 30 } } })
Enter fullscreen mode Exit fullscreen mode

Element Operators

Example documents:

{ name: "John", age: 30, address: { city: "New York", state: "NY" } }
{ name: "Jane", age: 25 }
{ name: "Bob", age: 40, address: null }
Enter fullscreen mode Exit fullscreen mode

Query examples:

  • $exists: find all documents where the address field exists
db.users.find({ address: { $exists: true } })
Enter fullscreen mode Exit fullscreen mode
  • $type: find all documents where the address field is of type "object"
db.users.find({ address: { $type: "object" } })
Enter fullscreen mode Exit fullscreen mode

Array Operators

Example documents:

{ name: "John", hobbies: ["reading", "hiking"] }
{ name: "Jane", hobbies: ["swimming", "yoga"] }
{ name: "Bob", hobbies: null }
Enter fullscreen mode Exit fullscreen mode

Query examples:

  • $all: find all documents where hobbies include both "reading" and "hiking"
db.users.find({ hobbies: { $all: ["reading", "hiking"] } })
Enter fullscreen mode Exit fullscreen mode
  • $elemMatch: selects documents if element(s) in an array field match the specified conditions.
db.inventory.find( { 
    sizes: { $elemMatch: { h: { $gt: 15 }, w: { $gt: 20 } } }, 
    status: "A" 
} )
Enter fullscreen mode Exit fullscreen mode
  • $size: selects documents if the array field is a specified size.
db.inventory.find( { tags: { $size: 3 } } )
Enter fullscreen mode Exit fullscreen mode

Bitwise Operators

  • $bitsAllSet: selects documents where all the specified bits are set.
db.users.find( { status: { $bitsAllSet: 16 } } )
Enter fullscreen mode Exit fullscreen mode
  • $bitsAnySet: selects documents where any of the specified bits are set.
db.users.find( { status: { $bitsAnySet: 1 } } )
Enter fullscreen mode Exit fullscreen mode

Evaluation Operators

  • $expr: allows the use of aggregation expressions within the query language.
db.sales.find( { $expr: { $gt: [ "$sales", "$goal" ] } } )
Enter fullscreen mode Exit fullscreen mode
  • $mod: performs a modulo operation on the value of a field and selects documents with a specified result.
db.inventory.find( { qty: { $mod: [ 4, 0 ] } } )
Enter fullscreen mode Exit fullscreen mode

Top comments (0)