ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Query Operators in MongoDB

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='5' AND `tutorial_submenu`='133' AND `tutorial_status`=1 LIMIT 1

Query Operators in MongoDB

Query Operators in MongoDB

MongoDB provides a powerful set of query operators that allow you to filter and match documents based on various conditions. These operators enable you to create complex queries and retrieve data based on criteria like comparison, logical operations, element presence, and more.

Common Query Operators

1. Comparison Operators

These operators allow you to perform comparisons between the field value and a given value.

  • $eq: Matches documents where the field equals the specified value.

    javascript

    db.users.find({ age: { $eq: 25 } })

  • $ne: Matches documents where the field does not equal the specified value.

    javascript

    db.users.find({ age: { $ne: 25 } })

  • $gt: Matches documents where the field is greater than the specified value.

    javascript

    db.users.find({ age: { $gt: 25 } })

  • $gte: Matches documents where the field is greater than or equal to the specified value.

    javascript

    db.users.find({ age: { $gte: 25 } })

  • $lt: Matches documents where the field is less than the specified value.

    javascript

    db.users.find({ age: { $lt: 25 } })

  • $lte: Matches documents where the field is less than or equal to the specified value.

    javascript

    db.users.find({ age: { $lte: 25 } })

  • $in: Matches documents where the field value is in a given array.

    javascript

    db.users.find({ age: { $in: [25, 30, 35] } })

  • $nin: Matches documents where the field value is not in a given array.

    javascrip

    db.users.find({ age: { $nin: [25, 30, 35] } })


2. Logical Operators

These operators allow you to combine multiple conditions using logical operators.

  • $and: Matches documents that satisfy all the specified conditions.

    javascript

    db.users.find({ $and: [{ age: { $gt: 25 } }, { age: { $lt: 35 } }] })

  • $or: Matches documents that satisfy at least one of the specified conditions.

    javascript

    db.users.find({ $or: [{ age: { $lt: 25 } }, { age: { $gt: 40 } }] })

  • $nor: Matches documents that fail all the specified conditions (opposite of $or).

    javascript

    db.users.find({ $nor: [{ age: { $lt: 25 } }, { age: { $gt: 40 } }] })

  • $not: Negates a condition (opposite of a given query).

    javascript

    db.users.find({ age: { $not: { $gte: 30 } } })


3. Element Operators

These operators match documents based on the presence or absence of fields.

  • $exists: Matches documents that have a field present or not present.

    javascript

    db.users.find({ email: { $exists: true } }) // Matches documents where 'email' field existsdb.users.find({ email: { $exists: false } }) // Matches documents where 'email' field does not exist

  • $type: Matches documents where the field is of the specified BSON type.

    javascript

    db.users.find({ age: { $type: "int" } }) // Matches documents where 'age' is of type integer

4. Array Operators

These operators help you query documents based on array values.

  • $all: Matches documents where the field is an array that contains all specified values.

    javascript

    db.users.find({ hobbies: { $all: ["reading", "travelling"] } })

  • $elemMatch: Matches documents where at least one element in an array matches the specified query.

    javascript

    db.users.find({ hobbies: { $elemMatch: { type: "sports", duration: { $gt: 10 } } } })

  • $size: Matches documents where the field is an array with the specified number of elements.

    javascript

    db.users.find({ hobbies: { $size: 3 } }) // Matches documents where 'hobbies' has 3 elements


5. Regular Expression Operator

  • $regex: Matches documents where the field value matches a regular expression pattern.

    javascript

    db.users.find({ name: { $regex: "^A" } }) // Matches documents where 'name' starts with 'A'

    You can also add options to the regular expression, such as i for case-insensitive matching:

    javascript

    db.users.find({ name: { $regex: "^a", $options: "i" } }) // Case-insensitive match for names starting with 'a'


6. Geospatial Query Operators

MongoDB provides operators to work with geospatial data (e.g., coordinates) and perform geospatial queries.

  • $near: Matches documents that are within a certain distance of a point.

    javascript

    db.locations.find({ location: { $near: { $geometry: { type: "Point", coordinates: [ -73.97, 40.77 ] }, $maxDistance: 1000 } }})

  • $geoWithin: Matches documents with geometries that are within a specified geometry.

    javascript

    db.locations.find({ location: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [...] } } }})

  • $geoIntersects: Matches documents where a field intersects with a specified geometry.

    javascript

    db.locations.find({ location: { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [...] } } }})


7. Projection Operators

MongoDB also provides operators to control which fields should be returned in the query results.

  • $slice: Limits the number of elements returned from an array.

    javascript

    db.users.find({}).project({ hobbies: { $slice: 3 } }) // Returns the first 3 elements of the 'hobbies' array

  • $elemMatch: Specifies that a single element in the array should be returned based on the specified criteria.

    javascript

    db.users.find({}).project({ hobbies: { $elemMatch: { type: "sports" } } }) // Returns array elements that match the condition


Combining Query Operators

MongoDB queries often combine multiple operators to create more complex conditions. For example, to find users who are between the ages of 25 and 40 and whose name starts with the letter 'A', you can combine multiple operators:

javascript

db.users.find({ $and: [ { age: { $gte: 25, $lte: 40 } }, { name: { $regex: "^A" } } ]})

Conclusion

MongoDB's query operators give you powerful ways to retrieve and filter data based on specific conditions. Whether you need to perform simple queries, use comparison operators, combine multiple conditions logically, or work with arrays and geospatial data, these operators make it easy to construct precise and efficient queries.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql