How to Find First Document in Collection in Python MongoDB


In this example we will show how to find the first document in the collection using the find_one() method in Python MongoDB.

Source Code

from pymongo import MongoClient

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

a = collection.find_one()
print(a)

Output:

{'_id': 1, 'State_name': 'Alabama', 'Abbreviation': 'AL', 'Capital': 'Montgomery', 'Total_area': 52420}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments