MongoDB Python驱动
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,使用pip install pymongo安装1.连接MongoDB实例In [60]: from pymongo import MongoClientIn [61]: client=MongoCli
千家信息网最后更新 2025年11月07日MongoDB Python驱动
使用pip install pymongo安装
1.连接MongoDB实例
In [60]: from pymongo import MongoClientIn [61]: client=MongoClient('mongodb://10.10.41.25:2911')In [62]: client=MongoClient('10.10.41.25',2911)两种写法都行
2.获取数据库信息
In [63]: db=client.gameIn [64]: db=client['game']
两种写法都行
3.获取集合信息
In [85]: collection=db.playerIn [86]: collection=db['player']
两种写法都行
4.插入一个文档记录
MongoDB以JSON格式存储和显示数据。在pymongo中以字典的方式显示数据。
In [95]: import datetimeIn [96]: post={"author":"Mike","text":"My first blog post!","tags":["mongodb","python","pymongo"],"date":datetime.datetime.utcnow()}In [132]: posts=db.postsIn [133]: post_id=posts.insert(post)In [134]: post_idOut[134]: ObjectId('550ad8677a50900165feae9d')当插入一个文档时,一个特殊的key,"_id"将自动添加到这个文档中。
In [136]: db.collection_names()Out[136]: [u'system.indexes',u'posts']
5.使用find_one()获取单个文档
In [141]: posts.find_one()Out[141]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'}In [142]: posts.find_one({"author":"Mike"})Out[142]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'}In [143]: posts.find_one({"author":"Eliot"})In [144]:MongoDB以BSON格式存储字符,而BSON字符串是以UTF-8编码,所以PyMongo必须要确保它存储的数据是有效的UTF-8编码的数据。常规字符串直接存储,但是经过编码的字符串首先以UTF-8编码存储。
6.使用ObjectID查找文档
In [151]: post_idOut[151]: ObjectId('550ad8677a50900165feae9d')In [152]: posts.find_one({"_id":post_id})Out[152]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'}ObjectID和它表示的字符串不一样
In [154]: post_id_as_str=str(post_id)In [155]: posts.find_one({"_id":post_id_as_str})没有任何结果显示
在一些WEB应用中,需要更加URL获取post_id进而根据post_id查找匹配的文档。在使用find_one()查找之前有必要将post_id从字符串转换成为ObjectID
7.批量插入文档数据
>>> new_posts = [{"author": "Mike",... "text": "Another post!", "tags": ["bulk", "insert"], "date": datetime.datetime(2009, 11, 12, 11, 14)}, {"author": "Eliot", "title": "MongoDB is fun", "text": "and pretty easy too!", "date": datetime.datetime(2009, 11, 10, 10, 45)}] >>> posts.insert(new_posts)[ObjectId('...'), ObjectId('...')]8.查询多个文档数据
In [165]: for post in posts.find(): post .....: .....: Out[166]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'}Out[166]: {u'_id': ObjectId('550b87d47a50907021e3473b'), u'author': u'Mike', u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!'}Out[166]: {u'_id': ObjectId('550b87d47a50907021e3473c'), u'author': u'Eliot', u'title': u'MongoDB is fun'}In [169]: for post in posts.find({"author" : "Mike"}): .....: post .....: .....: Out[169]: {u'_id': ObjectId('550ad8677a50900165feae9d'), u'author': u'Mike', u'date': datetime.datetime(2015, 3, 19, 14, 7, 14, 572000), u'tags': [u'mongodb', u'python', u'pymongo'], u'text': u'My first blog post!'}Out[169]: {u'_id': ObjectId('550b87d47a50907021e3473b'), u'author': u'Mike', u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!'}9.总计
In [170]: posts.count()Out[170]: 3In [171]: posts.find({"author":"Mike"}).count()Out[171]: 210.范围查询
In [183]: d=datetime.datetime(2009,11,12,12)In [184]: for post in posts.find({"date":{"$lt":d}}).sort("author"): .....: print post .....: .....: {u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!', u'_id': ObjectId('550b87d47a50907021e3473b'), u'author': u'Mike'}11.索引
使用索引可以加快查询速度,缩小查询范围。
In [201]: posts.find({"date" : {"$lt":d}}).sort("author").explain()["cursor"]Out[201]: u'BasicCursor'In [202]: posts.find({"date" : {"$lt":d}}).sort("author").explain()["nscanned"]Out[202]: 3创建组合索引
In [241]: from pymongo import ASCENDING,DESCENDINGIn [242]: posts.create_index([("date",DESCENDING),("author",ASCENDING)])Out[242]: u'date_-1_author_1'In [243]: posts.find({"date" : {"$lt":d}}).sort("author").explain()["nscanned"]Out[243]: 112.
参考文档
http://api.mongodb.org/python/current/tutorial.html?_ga=1.58141740.722641156.1410499072
文档
数据
字符
字符串
存储
编码
查询
写法
索引
UTF-8
信息
格式
范围
有效
特殊
必要
单个
多个
字典
实例
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
atm是无连接的网络技术
杨浦区智能化软件开发采购
软件开发季度述职模板
天气预报app抓取数据库
青少年网络安全举报
羊城杯网络安全2021
服务器机柜安装教程图
布兰卡德服务器
互联网科技的发展状况
2021软考数据库答案上午
广州科技互联网学院
手机改成流媒体服务器
服务器忘记密码怎样登录
深圳远云互联网科技有些公司
百度爬虫技术抓取电信数据库
常用网络安全工具
青岛泰捷网络技术有限公司
数据库图形工具
欧瑞服务器显示AL01
ora连接数据库被拒绝访问
互联网未来新科技
软件开发前景好还是金融好
战地4与服务器
误造怎么单独开服务器
青海双路机架服务器什么价位
少儿校园网络安全文明用语
江苏省扬州网络技术公司
网络服务器设置自动获取
春节前信息网络安全大检查
app显示服务器失联了