Heim  >  Artikel  >  Backend-Entwicklung  >  python操作mongodb根据_id查询数据的实现方法

python操作mongodb根据_id查询数据的实现方法

WBOY
WBOYOriginal
2016-06-06 11:16:361355Durchsuche

本文实例讲述了python操作mongodb根据_id查询数据的实现方法。分享给大家供大家参考。具体分析如下:

_id是mongodb自动生成的id,其类型为ObjectId,所以如果需要在python中通过_id查询,就需要转换类型

如果pymongo的版本号小于2.2,使用下面的语句导入ObjectId

from pymongo.objectid import ObjectId

如果pymongo的版本号大于2.2,则使用下面的语句

from bson.objectid import ObjectId

查询代码如下:

collection.find_one({'_id':ObjectId('50f0d76347f4ec148890ef1e')})

希望本文所述对大家的Python程序设计有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn