Home > Backend Development > Python Tutorial > python操作mongodb根据_id查询数据的实现方法

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

WBOY
Release: 2016-06-06 11:16:36
Original
1397 people have browsed it

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

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

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

from pymongo.objectid import ObjectId
Copy after login

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

from bson.objectid import ObjectId
Copy after login

查询代码如下:

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template