Python操作MongoDB查询时处理ObjectId

从bson中导入ObjectId对象,将字符串转换成id对象查询使用:

from bson import ObjectId
import pymongo
conn = pymongo.MongoClient('mongodb://root:abc123@127.0.0.1/admin'
str = "5sdfadsfasdfxzcasdfasdfas"
findinfo = conn.db_name.table_name.find({"_id" : ObjectId(str)})

 

posted @ 2019-09-22 17:41  撒欢  阅读(4863)  评论(0编辑  收藏  举报