Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试这样做:
event = coll.find_one({"_id": {"$oid": event_id}})
其中 event_id 是来自 ObjectId 的字符串:
event_id='50bbd48eefbdd2a1e83bc440'
但事件最终是空的。我确定这不是您在 pymongo 中查找具有 ObjectId 的文档的方式,但我已经搜索了一段时间,但还没有弄清楚如何。任何帮助,将不胜感激。谢谢!
你必须转换event_id成ObjectId这样的:
event_id
ObjectId
from bson.objectid import ObjectId event_id='50bbd48eefbdd2a1e83bc440' event = coll.find_one({"_id": ObjectId(event_id)})
event_id当使用您定义的 ObjectId 字符串按 id 搜索时,只需执行
event = coll.find_one(event_id)