Python MongoDB – Query

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” :{$lte:”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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing Mongoclient from pymongo
from pymongo import MongoClient 
  
  
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/"
   
# database 
db = myclient["mydatabase"]
   
# Created or Switched to collection
# names: GeeksForGeeks
Collection = db["GeeksForGeeks"]
  
# Filtering the Quantities greater
# than 40 using query.
cursor = Collection.find({"Quantity":{"$gt":40}})
  
# Printing the filterd data.
print("The data having Quantity greater than 40 is:")
for record in cursor: 
    print(record) 
      
# Filtering the Quantities less 
# than 40 using query.
cursor = Collection.find({"Quantity":{"$lt":40}})
  
# Printing the filterd data.
print("\nThe data having Quantity less than 40 is:")
for record in cursor: 
    print(record) 

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing Mongoclient from pymongo
from pymongo import MongoClient 
  
  
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/"
   
# database 
db = myclient["mydatabase"]
   
# Created or Switched to collection 
# names: GeeksForGeeks
Collection = db["GeeksForGeeks"]
  
# Filtering the (Quantities greater than
# 40 AND greater than 40) using AND query.
cursor = Collection.find({"$and":[{"Quantity":{"$gt":40}},
                                  {"Quantity":{"$gt":50}}]})
  
# Printing the filterd data.
print("Quantities greater than 40 AND\
Quantities greater than 40 :")
for record in cursor: 
    print(record) 
  
# Filtering the (Quantities greater than
# 40 OR greater than 40) using OR query.
cursor = Collection.find({"$or":[{"Quantity":{"$gt":40}},
                                 {"Quantity":{"$gt":50}}]})
  
# Printing the filterd data.
print()
print("Quantities greater than 40 OR\
Quantities greater than 40 :")
for record in cursor: 
    print(record) 

chevron_right


Output:




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.