依靠pymongo包来对mongoDB数据库进行操作
一、pymongo安装
首先通过pip list或者通过import pymongo,查看自己装没装pymongo。
出错提示版本匹配有问题。
通过Anaconda中安装pymongo环境,失败。
重新更新pip版本
1
| python -m pip install --upgrade pip
|
更新成功后,再pip install pymongo,成功!!
二、pymongo基本操作
0.简易操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import pymongo import json
MONGODB_HOST = ''
MONGODB_PORT = 27017
MONGODB_DBNAME = ''
MONGODB_DOCNAME = ''
MONGODB_USERNAME = ''
MONGODB_PASSWORD = ''
client = pymongo.MongoClient(host=MONGODB_HOST, port=MONGODB_PORT) mdb = client[MONGODB_DBNAME]
mdb.authenticate(MONGODB_USERNAME, MONGODB_PASSWORD) coll = mdb[doc] data = coll.find({}) for d in data: print(d)
|
1.创建数据库
连接名["库名"]
1 2 3 4
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoob"]
|
2.获取数据库列表
连接名.list_database_names()
1 2 3 4 5 6
| import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/')
dblist = myclient.list_database_names() if "runoobdb" in dblist: print("数据库已存在!")
|
3.创建集合(表)
库名["集合名"]
1 2 3 4 5
| import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/') mydb = myclient["runoob"]
mycol = mydb["sites"]
|
4.获取集合列表
库名.list_collection_names()
1 2 3 4 5 6 7
| import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/') mydb = myclient['runoobdb']
collist = mydb.list_collection_names() if "sites" in collist: print("集合已存在!")
|
5.插入文档
5.1 插入单个文档
集合名.inset_one()
该方法返回 InsertOneResult 对象,该对象包含 inserted_id 属性,它是插入文档的 id 值。
1 2 3 4 5 6 7 8 9
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
mydict = {"name":"RUNOOB","alexa":"10000","url":"https://www.runoob.com"} x = mycol.insert_one(mydict) print(x) print(x.inserted_id)
|
5.2 插入多个文档
集合名.inset_many()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
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)
print(x.inserted_ids)
|
5.2 插入多个文档(指定_id)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["site2"]
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)
print(x.inserted_ids)
|
6.查询文档
6.1 查询单个文档
集合名.find_one()
1 2 3 4 5 6 7 8 9
| import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
x = mycol.find_one()
print(x)
|
6.2 查询所有文档
集合名.find()
1 2 3 4 5 6 7 8 9 10
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017") mydb = myclient["runoob"] mycol = mydb["sites"]
for x in mycol.find() print(x) for y in mycol.find({},{"_id":0,"title":1}) print(y)
|
==除了 _id ,你不能在一个对象中同时指定 0 和 1(如{ “name”: 1, “alexa”: 0 }),如果你设置了一个字段为 0,则其他都为 1,反之亦然。==
6.3 查询指定条件文档
1 2 3 4 5 6 7 8 9 10 11
| import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
myquery = { "name": "RUNOOB" } mydoc = mycol.find(myquery)
for x in mydoc: print(x)
|
6.4 高级查询
基本和mongoDB中写法相同。
1 2 3 4 5 6 7 8 9 10 11 12
| import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
myquery = { "name": { "$gt": "H" } } mydoc = mycol.find(myquery) for x in mydoc: print(x)
|
6.5 使用正则表达式匹配字符串
1 2 3 4 5 6 7 8 9 10 11 12
| import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
myquery = { "name": { "$regex": "^R" } }
mydoc = mycol.find(myquery)
for x in mydoc: print(x)
|
6.6 返回指定记录数
集合名.find().limit(num)
1 2 3 4 5 6 7 8 9 10 11
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
myresult = mycol.find().limit(3)
for x in myresult: print(x)
|
7.修改文档
7.1 修改单个文档
集合名.update_one()
1 2 3 4 5 6 7 8 9 10 11 12 13
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
myquery = { "alexa": "10000" } newvalues = { "$set": { "alexa": "12345" } }
mycol.update_one(myquery, newvalues)
for x in mycol.find(): print(x)
|
7.1 修改多个文档
集合名.update_many()
1 2 3 4 5 6 7 8 9 10 11 12
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
myquery = { "alexa": { "$reget":"^F"} } newvalues = { "$set": { "alexa": "123" } }
x = mycol.update_many(myquery, newvalues)
print(x.modified_count, "文档已修改")
|
8.排序文档
集合名.find().sort()
1 2 3 4 5 6 7 8
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
mydoc = mycol.find().sort("alexa",-1) for x in mydoc: print(x)
|
9.删除数据
9.1 删除单个文档
集合名.delete_one()
1 2 3 4 5 6 7 8 9 10 11
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"] myquery = { "name": "Taobao" }
mycol.delete_one(myquery)
for x in mycol.find(): print(x)
|
9.2 删除多个文档
集合名.delete_many()
1 2 3 4 5 6 7 8 9 10
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"] myquery = { "name": {"$reget":"^F"} }
x = mycol.delete_many(myquery)
print(x.deleted_count, "个文档已删除")
|
9.3 删除所有文档
集合名.delete_many()
1 2 3 4 5 6 7 8 9
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
x = mycol.delete_many({})
print(x.deleted_count, "个文档已删除")
|
9.4 删除集合
集合名.drop()
1 2 3 4 5 6
| import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"]
mycol.drop()
|
最后更新时间: