Alexander
Alexander

Reputation: 111

Python MongoDB Find One

I am trying to find by id a document in the database, but I get None. What am I doing wrong?

python:

card = mongo.db['grl'].find_one({'id': 448510476})

or:

card = mongo.db['grl'].find_one({'id': '448510476'})

document:

{"_id":{"$oid":"5f25b1d787fc4c34a7d9aabe"},
"id":{"$numberInt":"448510476"},"first_name":"Arc","last_name":"Fl"}

enter image description here

Upvotes: 2

Views: 5327

Answers (1)

WMRamadan
WMRamadan

Reputation: 1210

I'm not sure how you are initializing your database but try this:

from pymongo import MongoClient

client = MongoClient("mongodb://127.0.0.1:27017")
db = client.database #Selecting database named "database"

#find one in collection named "collection"    
card = db.collection.find_one({"id": "448510476"})

print(card)

Upvotes: 2

Related Questions