How to Specify Query Criteria When Querying in Python MongoDB


In this example we will show how to specify query criteria when querying using the find() method in Python MongoDB.

Source Code

from pymongo import MongoClient

client = MongoClient("localhost", 27017)
db = client["mydatabase"]
collection = db["us_state"]

query = {"Capital": "Dover"}

for x in collection.find(query):
    print(x)

Output:

{'_id': 8, 'State_name': 'Delaware', 'Abbreviation': 'DE', 'Capital': 'Dover', 'Total_area': 2489}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments