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')}
复制代码
posted @ 2020-06-10 00:24  极地阳光-ing  阅读(191)  评论(0编辑  收藏  举报