pymongo模块
连接mongodb
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017") # dblist = myclient.list_database_names() # 列出所有库 mydb = myclient['cmdb_t'] # collist = mydb.list_collection_names() # 列出所有库中的表collection mycol = mydb['rec_project']
insert操作
# 插入一行数据 mydict = {'k1':'v1','k2':'v2'} x = mycol.insert_one(mydict) print(x.inserted_id) # 返回值为插入的数据id # 插入多行数据 mylist = [ { "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" }, { "name": "QQ", "alexa": "101", "url": "https://www.qq.com" }, { "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" }, { "name": "知乎", "alexa": "103", "url": "https://www.zhihu.com" }, { "name": "Github", "alexa": "109", "url": "https://www.github.com" } ] x = mycol.insert_many(mylist) # 输出插入的所有文档对应的 _id 值 print(x.inserted_ids) # 插入指定_id的多行数据 mylist = [ { "_id": 1, "name": "RUNOOB", "cn_name": "菜鸟教程"}, { "_id": 2, "name": "Google", "address": "Google 搜索"}, { "_id": 3, "name": "Facebook", "address": "脸书"}, { "_id": 4, "name": "Taobao", "address": "淘宝"}, { "_id": 5, "name": "Zhihu", "address": "知乎"} ] x = mycol.insert_many(mylist) # 输出插入的所有文档对应的 _id 值 print(x.inserted_ids)
update操作
myquery = {"alexa": "100"} newvalue = {"$set":{"alexa": "12345"}} mycol.update(myquery,newvalue) for x in mycol.find(): print(x) myquery = {"alexa": {"$regex":"^10"}} newvalue = {"$set":{"alexa": "12345"}} mycol.update(myquery,newvalue) # 只改一个 for x in mycol.find(): print(x) myquery = {"alexa": {"$regex":"^123"}} newvalue = {"$set":{"alexa": "54321"}} mycol.update_one(myquery,newvalue) # 只改一个 for x in mycol.find(): print(x) myquery = {"alexa": {"$regex":"^10"}} newvalue = {"$set":{"alexa": "221010"}} mycol.update_many(myquery,newvalue) # 改所有 for x in mycol.find(): print(x)
find操作
# 查看一条数据 x = mycol.find_one() print(x) # 查看所有数据 _all = mycol.find() for i in _all: print(i) # 指定返回字段,指定字段是否返回要一致,要么都指定1,要么都指定0,只_id字段例外 _all = mycol.find({},{"_id":0,"name":1}) for i in _all: print(i) mycol.find({},{ "_id": 0 }) 除了id字段都返回 # 指定条件查询 myquery = { "name": "RUNOOB" } # = mydoc = mycol.find(myquery) for i in mydoc: print(i) myquery = { "name": { "$gt": "H" } } # > mydoc = mycol.find(myquery) for i in mydoc: print(i) # 正则 myquery = { "name": { "$regex": "^R" } } mydoc = mycol.find(myquery) for x in mydoc: print(x)
myresult = mycol.find().limit(3) # limit for x in myresult: print(x) myresult = mycol.find().sort('alexa') # sort for x in myresult: print(x) myresult = mycol.find().sort('alexa',-1) # sort desc for x in myresult: print(x)
delete操作
mycol.delete_one({'name':'Taobao'}) # 删除一个 for x in mycol.find():print(x) r = mycol.delete_many({'name':{"$regex":"^F"}}) # 删除多个并返回r print(r.deleted_count) # 查看删除的数据条数 x = mycol.delete_many({}) # 删除所有 print(x.deleted_count, u"个文档已删除") mycol.drop() # 删除当前集合,相当于删表
批量操作
from pymongo import InsertOne, DeleteOne, ReplaceOne requests = [InsertOne({'y': 1}), DeleteOne({'x': 1}),ReplaceOne({'w': 1}, {'z': 1}, upsert=True)] result = db.test.bulk_write(requests)
>>> result.inserted_count 1 >>> result.deleted_count 1 >>> result.modified_count 0 >>> result.upserted_ids {2: ObjectId('54f62ee28891e756a6e1abd5')}
本文来自博客园,作者:极地阳光-ing,转载请注明原文链接:https://www.cnblogs.com/Polar-sunshine/p/13082156.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
· 在外漂泊的这几年总结和感悟,展望未来
· 博客园 & 1Panel 联合终身会员上线
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· https证书一键自动续期,帮你解放90天限制
· 在 ASP.NET Core WebAPI如何实现版本控制?