how to query mongo data in python
时间: 2024-02-20 17:55:59 浏览: 24
To query MongoDB data in Python, you can use the PyMongo library. Here's an example of querying a MongoDB collection for documents that match a specific criteria:
```python
import pymongo
# connect to the MongoDB server
client = pymongo.MongoClient("<mongodb_uri>")
# select the database and collection
db = client["mydatabase"]
col = db["mycollection"]
# define the query criteria
query = { "name": "John" }
# execute the query and retrieve the matching documents
results = col.find(query)
# iterate over the results and print them
for doc in results:
print(doc)
```
In this example, replace `<mongodb_uri>` with the URI of your MongoDB server, `"mydatabase"` with the name of your database, `"mycollection"` with the name of your collection, and `"name": "John"` with the criteria you want to use for the query.