EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Query Operators
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

MongoDB Query Operators

MongoDB Query Operators

Definition of MongoDB Query Operators

MongoDB query operators are used to interact with the database using queries, there are different types of query operators are available in MongoDB. MongoDB query operators are nothing but keywords or special symbols which informed the interpreter to process the logical or mathematical operations. Basically, the query operator is enhanced more MongoDB functionality which allowed the user to create more complex queries and interacts with the data set. We have using the find command to fetch the data from collections using the query operator, also we need to use prefix as “$” sign before the query operators.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Below is the syntax of query operators in MongoDB.

db.name_of_collection.find ( { “Field_name”: { $query_operator_name: “value” } } ) .pretty()

Parameter description syntax of query operators in MongoDB.

1) Name of collection – This parameter is defined as the collection name from which we have retrieving documents as per the query operators which was we have used in our query.
2) Find – This method is used to retrieve data from collections. We can also use find method with query operator to retrieve specific documents in MongoDB.
3) Field name – This parameter is defined as the name of the field from which we have retrieving data as per the query parameter which was we have used in our query.
4) Query operator name – We need to use specified query operator name to retrieve documents from collection as per the query operations.
5) Value – The value nothing but the field value which was we have using with the query operator to perform the logical or mathematical operations on it.
6) Pretty – Basically MongoDB output is in an unstructured format using this method we can display our output in a structured format.

How query operators work in MongoDB?

  • We can use MongoDB query operators in any command which was supported by MongoDB.
  • Following are the types of query operators which was available in MongoDB.

1) Comments
2) Bitwise
3) Array
4) Comparison
5) Logical
6) Element
7) Evaluation
8) Geospatial

  • MongoDB comparison operator is used to compare the value from documents. Below are the comparison query operators available in MongoDB.

1) Equal to ($eq)
2) Greater than ($gt)
3) Less than ($lt)
4) Greater or equal to ($gte)
5) Less or equal to ($lte)
6) Match value from array ($in)
7) Not equal ($ne)
8) Match none of the value from array ($nin)

  • Logical operator in MongoDB is used to filter data as per given conditions. Below is the type of logical query operators.

1) Logical AND ($and)
2) Logical OR ($or)
3) Logical NOR ($nor)
4) Logical NOT ($not)

  • Element query operators is used in MongoDB to identify the documents as per the documents field.

1) $exists – Match document as per specified field.
2) $type – Match document using the specified field.

  • MongoDB evaluation query operators will used to evaluate the individual field or overall data structure from document.
  • Below is the evaluation query operator available in MongoDB is as follows.

1) $jsonSchema – This operator is used to validate documents as per schema.
2) $mod – It will match the document when our field value is equal with the remainder value.
3) $regex – It will match the document as per our regular expression.
4) $text – It will perform the text search on the given field.
5) $where – It will match the document which satisfied the JavaScript expression.

  • Array operators in MongoDB are basically designed for arrays with query documents. Below is the array query operator in MongoDB.

1) $all – It will match all the elements from array which satisfy the given condition.
2) $size – It will match the array as per specified size is equal to the array size.
3) $elemMatch – It will match the documents as per specified elemMatch condition.

  • The last type of query operator in MongoDB is the comment operator. This is used to give comments to any expression or query. If we add comments to any query it will be easy for any people to trace or see the logs.

Example

Below is the example of a query operator in MongoDB.

1) MongoDB comparison query operator

Below example shows MongoDB comparison query operator. In following example we have used $eq, $gt, $lt, $gte and $lte query operator.

Code:

db.query_operator.find ()
db.query_operator.find ( { stud_id: { $eq: 1 } } )
db.query_operator.find ( { stud_id: { $gt: 2 } } )
db.query_operator.find ( { stud_id: { $lt: 2 } } )
db.query_operator.find ( {stud_id: { $lte: 2 } } )
db.query_operator.find ( { stud_id: { $gte: 2 } } )

MongoDB query operator 1

MongoDB query operator 2

Figure – Example of comparison query operator in MongoDB.

2) MongoDB logical query operator

The below example shows MongoDB logical query operator. In following example we have used $and, $or and $nor query operator.

Code:

db.query_operator.find ()
db.query_operator.find ( { $and: [{stud_name: "ABC" }, { marks: { $gte: 70 } } ] } )
db.query_operator.find ( { $or: [ { stud_id: 1 }, { stud_name: "ABC" } ] } )
db.query_operator.find ( { $nor: [ { stud_id: 1 }, { stud_name: "ABC" } ] } )

MongoDB query operator 3

Figure – Example of logical query operator in MongoDB.

3) MongoDB element query operator

The below example shows MongoDB element query operator. In following example we have used $exists and $type query operator.

Code:

db.query_operator.find ()
db.query_operator.find ( { "marks": { $exists: true, $gte: 50 } } )
db.query_operator.find ( { "marks": { $type: "double" } } )

MongoDB query operator 4

MongoDB query operator 5

Figure – Example of element query operator in MongoDB.

4) MongoDB evaluation query operator

The below example shows MongoDB evaluation, query operator. In the following example, we have used $mod and $text query operator.

Code:

db.query_operator.find ()
db.query_operator.find ( { "stud_name": { $regex: '.B.' } } )
db.query_operator.createIndex ( { "stud_name": "text" } )

example 5

example 5-1

Figure – Example of evaluation query operator in MongoDB.

5) MongoDB array query operator

The below example shows MongoDB array, query operator. In the following example, we have used $all and $size query operators.

Code:

db.query_operator.find ()
db.query_operator.find ( { "marks": { $all: [ 60, 65 ] } } )
db.query_operator.find ( { "marks": { $size: 5 } } )

example 6

image

Figure – Example of array query operator in MongoDB.

Conclusion

Query operator is useful and important in MongoDB. MongoDB query operators are nothing but the keywords or special symbols which was used to inform the interpreter for processing logical or mathematical operations. Comparison, logical, element, evaluation, geospatial, array, bitwise, and comments are the types of query operator in MongoDB.

Recommended Articles

This is a guide to MongoDB Query Operators. Here we discuss the definition, How query operators work in MongoDB? and examples, respectively. You may also have a look at the following articles to learn more –

  1. MongoDB group by
  2. MongoDB Timestamp
  3. MongoDB Import
  4. MongoDB GridFS
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more