MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs.
What is a MongoDB Query?
MongoDB query is used to specify the selection filter using query operators while retrieving the data from the collection by db.find() method. We can easily filter the documents using the query object. To apply the filter on the collection, we can pass the query specifying the condition for the required documents as a parameter to this method, which is an optional parameter for db.find() method.
Query Selectors: Following is the list of some operators used in the queries in MongoDB.
Operation |
Syntax |
Description |
Equality |
{“key” : “value”} |
Matches values that are equal to a specified value. |
Less Than |
{“key” :{$lt:”value”}} |
Matches values that are less than a specified value. |
Greater Than |
{“key” :{$gt:”value”}} |
Matches values that are greater than a specified value. |
Less Than Equal to |
{“key” :{$lte:”value”}} |
Matches values that are less than or equal to a specified value. |
Greater Than Equal to |
{“key” :{$gte:”value”}} |
Matches values that are greater than or equal to a specified value. |
Not Equal to |
{“key”:{$ne: “value”}} |
Matches all values that are not equal to a specified value. |
Logical AND |
{ “$and”:[{exp1}, {exp2}, …, {expN}] } |
Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
Logical OR |
{ “$or”:[{exp1}, {<exp2}, …, {expN}] } |
Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
Logical NOT |
{ “$not”:[{exp1}, {exp2}, …, {expN}] } |
Inverts the effect of a query expression and returns documents that do not match the query expression. |
The Database or Collection on which we operate:
Example 1:
Python3
from pymongo import MongoClient
myclient = MongoClient("mongodb: / / localhost: 27017 / ")
db = myclient["mydatabase"]
Collection = db["GeeksForGeeks"]
cursor = Collection.find({"Quantity":{"$gt": 40 }})
print ("The data having Quantity greater than 40 is :")
for record in cursor:
print (record)
cursor = Collection.find({"Quantity":{"$lt": 40 }})
print ("\nThe data having Quantity less than 40 is :")
for record in cursor:
print (record)
|
Output:

Example 2:
Python3
from pymongo import MongoClient
myclient = MongoClient("mongodb: / / localhost: 27017 / ")
db = myclient["mydatabase"]
Collection = db["GeeksForGeeks"]
cursor = Collection.find({"$ and ":[{"Quantity":{"$gt": 40 }},
{"Quantity":{"$gt": 50 }}]})
print ("Quantities greater than 40 AND\
Quantities greater than 40 :")
for record in cursor:
print (record)
cursor = Collection.find({"$ or ":[{"Quantity":{"$gt": 40 }},
{"Quantity":{"$gt": 50 }}]})
print ()
print ("Quantities greater than 40 OR\
Quantities greater than 40 :")
for record in cursor:
print (record)
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
15 Mar, 2023
Like Article
Save Article