Python MongoDB – Query

01 Apr, 2022
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.
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. 
.math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-child(odd) { background-color: #ffffff; } 
The Database or Collection on which we operate:  Example 1: 
Python3
# importing Mongoclient from pymongofrom pymongo import MongoClient  # Making Connectionmyclient = MongoClient("mongodb://localhost:27017/")  # databasedb = myclient["mydatabase"]  # Created or Switched to collection# names: GeeksForGeeksCollection = db["GeeksForGeeks"] # Filtering the Quantities greater# than 40 using query.cursor = Collection.find({"Quantity":{"$gt":40}}) # Printing the filtered 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 filtered data.print("\nThe data having Quantity less than 40 is:")for record in cursor:    print(record)
Output:  Example 2: 
Python3
# importing Mongoclient from pymongofrom pymongo import MongoClient  # Making Connectionmyclient = MongoClient("mongodb://localhost:27017/")  # databasedb = myclient["mydatabase"]  # Created or Switched to collection# names: GeeksForGeeksCollection = 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 filtered 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 filtered data.print()print("Quantities greater than 40 OR\Quantities greater than 40 :")for record in cursor:    print(record)
Output: 
rkbhola5
Python-mongoDB
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Iterate over a list in Python