I need to search an ObjectId with python using pymongo but I always get an error.
import pymongo
from pymongo import MongoClient
from pymongo import ObjectId
gate = collection.find({'_id': ObjectId(modem["dis_imei"])})
Any ideas how to search?
I need to search an ObjectId with python using pymongo but I always get an error.
import pymongo
from pymongo import MongoClient
from pymongo import ObjectId
gate = collection.find({'_id': ObjectId(modem["dis_imei"])})
Any ideas how to search?
I use pymongo 2.4.1.
from bson.objectid import ObjectId
[i for i in dbm.neo_nodes.find({"_id": ObjectId(obj_id_to_find)})]
obj_id_to_find
into a string i.e. str(obj_id_to_find)
–
Homophonous Assuming each document has a unique ObjectId
, we can directly search for it using find_one
. An example would look like:
from bson import ObjectId
collection.find_one(ObjectId("1234ab567c890d111213efgh"))
Note that unlike collection.find({})
which returns a cursor (so has to be iterated over to get the actual document), the above call returns either dict (if there is a match) or None (if there is no match).
© 2022 - 2024 — McMap. All rights reserved.