使用 Python 使用字符串查询 MongoDB 的 _id 的正确方法是什么? [英] What is the correct way to query MongoDB for _id using string by using Python?

查看:89
本文介绍了使用 Python 使用字符串查询 MongoDB 的 _id 的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pymongo 驱动程序.据说,可以使用字符串来查询文档的 _id 字段,如下所示:

I am using pymongo driver. Supposedly, one can use a string to query the _id field of a document, like this:

thing = db.things.find_one({'_id':'4ea113d6b684853c8e000001'})

但它不起作用.我做错了什么?

But it doesn't work. What am I doing wrong?

推荐答案

应该是:

from pymongo.objectid import ObjectId   
thing = db.things.find_one({'_id': ObjectId('4ea113d6b684853c8e000001') })

当前的导入是:from bson.objectid import ObjectId

这篇关于使用 Python 使用字符串查询 MongoDB 的 _id 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆