一、pymongo库的基本操作
1.终端窗口设置要使用的数据库权限
> use nosql
switched to db nosql
> db.createUser({user:'root',pwd:'123456',roles:['readWrite']})
Successfully added user: { "user" : "root", "roles" : [ "readWrite" ] }
> db.auth("root","*****")
1
注:①*****为密码
②pymongo连接哪个数据库就先要在终端设置数据库权限
③nosql是需要使用的数据库名,此处以nosql为例
2.创建mongodb客户端对象
from pymongo import MongoClient
client = MongoClient([host=“127.0.0.1”,port=27017])
注:服务器在本地时可不用写host和port地址
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017) # 连接数据库
client.nosql.authenticate("root","*****",mechanism='SCRAM-SHA-1') # 权限验证
2.创建/选择数据库以及集合
collection = client[“数据库名”][“集合名”]
注:如果存在此数据库或者集合,则选择此数据库或者集合,如果不存在则创建数据库或集合
collection = client['nosql']['stu'] # 选择数据库和集合
3.向集合中插入数据
- 插入一条数据(返回值为一个对象)
collection.insert_one({“字段1名”:“值”,“字段2名”:“值”})
- 插入多条数据(返回值为一个对象)
collection.insert_many([{“字段1名”:“值”,“字段2名”:“值”},{“字段1名”:“值”,“字段2名”:“值”},{…},…])
#插入一条数据
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017)
client.nosql.authenticate("root","123456",mechanism='SCRAM-SHA-1')
collection = client['nosql']['stu']
# 插入一条数据
ret = collection.insert_one({"name":"test01"})
print(type(ret))
print(list(collection.find()))
运行结果
<class 'pymongo.results.InsertOneResult'>
[{'_id': ObjectId('5e92d3a1c4c667c3c12535ab'), 'name': 'test01'}]
#插入多条数据
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017)
client.nosql.authenticate("root","123456",mechanism='SCRAM-SHA-1')
collection = client['nosql']['stu']
# 数据列表
data_list = [{"name":"test{}".format(i)} for i in range(10)]
ret = collection.insert_many(data_list)
print(type(ret))
print(list(collection.find()))
运行结果
<class 'pymongo.results.InsertManyResult'>
[{'_id': ObjectId('5e92d459c4c667c3c12535ad'), 'name': 'test01'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b0'), 'name': 'test0'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b1'), 'name': 'test1'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b2'), 'name': 'test2'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b3'), 'name': 'test3'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b4'), 'name': 'test4'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b5'), 'name': 'test5'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b6'), 'name': 'test6'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b7'), 'name': 'test7'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b8'), 'name': 'test8'}, {'_id': ObjectId('5e92d4d3c4c667c3c12535b9'), 'name': 'test9'}]
4.从集合中删除数据
- 删除一条数据(只删除符合条件的第一条数据,返回一个对象)
collection.delete_one({“字段名”:“值”})
- 删除多条数据(删除符合条件的所有数据,返回一个对象)
collection.delete_many({“字段名”:“值”})
- 删除所有数据
collection.delete_many({})
#删除一条数据
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017)
client.nosql.authenticate("root","123456",mechanism='SCRAM-SHA-1')
collection = client['nosql']['stu']
ret = collection.insert_one({"name":"test01"})
print(type(ret))
print(list(collection.find()))
ret = collection.delete_one({"name":"test01"})
print(list(collection.find()))
运行结果
#删除多条数据
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017)
client.nosql.authenticate("root","123456",mechanism='SCRAM-SHA-1')
collection = client['nosql']['stu']
collection.insert_one({"name":"test01","age":18})
collection.insert_one({"name":"test01","age":19})
print(list(collection.find()))
ret = collection.delete_many({"name":"test01"})
print(list(collection.find()))
运行结果
5.修改集合中的数据
- 修改一条数据(只修改符合条件的第一条数据,返回一个对象)
collection.update_one({“原字段名”:“原值”},{"$set":{“新字段名”,“新值”}})
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017)
client.nosql.authenticate("root","123456",mechanism='SCRAM-SHA-1')
collection = client['nosql']['stu']
print(list(collection.find()))
collection.update_one({"name":"test0"},{"$set":{"name":"test10"}})
print(list(collection.find()))
运行结果
- 修改多条数据(修改匹配到的所有数据,返回一个对象)
collection.update_many({“原字段名”:“原值”},{“$set”:{“新字段名”,“新值”}})
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017)
client.nosql.authenticate("root","123456",mechanism='SCRAM-SHA-1')
collection = client['nosql']['stu']
data_list = [{"name":"test10"} for i in range(10)]
collection.insert_many(data_list)
print(list(collection.find()))
ret = collection.update_many({"name":"test10"},{"$set":{"name":"test"}})
print(ret)
print(list(collection.find()))
运行结果
6.查找集合中的数据
- 查询一条数据(返回符合条件的第一个数据,返回值为一个对象,使用for循环或者转换为列表可查看)
collection.find_one({“字段名”:“值”})
- 查询多条数据(返回符合条件的所有数据,返回值为一个对象,使用for循环或者转换为列表可查看)
collection.find({“字段名”:“值”})
- 查询所有数据(返回值为一个对象,使用for循环或者转换为列表可查看)
- collection.find()
#查询一条数据
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017)
client.nosql.authenticate("root","123456",mechanism='SCRAM-SHA-1')
collection = client['nosql']['stu']
ret = collection.find_one({"name":"test"})
print(ret)
for temp in ret:
print(temp)
print("*" * 20)
print(list(collection.find_one({"name":"test"})))
运行结果
{'_id': ObjectId('5e92ec2dc4c667c3c12535c9'), 'name': 'test'}
_id
name
********************
['_id', 'name']
#查找多条数据
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1",port=27017)
client.nosql.authenticate("root","123456",mechanism='SCRAM-SHA-1')
collection = client['nosql']['stu']
ret = collection.find({"name":"test"})
print(ret)
for temp in ret:
print(temp)
print("*" * 20)
print(list(collection.find({"name":"test"})))
运行结果
pymongo.cursor.Cursor object at 0x000001E1B68ABA90>
{'_id': ObjectId('5e92ec2dc4c667c3c12535c9'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535ca'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535cb'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535cc'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535cd'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535ce'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535cf'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535d0'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535d1'), 'name': 'test'}
{'_id': ObjectId('5e92ec2dc4c667c3c12535d2'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535d4'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535d5'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535d6'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535d7'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535d8'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535d9'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535da'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535db'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535dc'), 'name': 'test'}
{'_id': ObjectId('5e92ec5ac4c667c3c12535dd'), 'name': 'test'}
********************
[{'_id': ObjectId('5e92ec2dc4c667c3c12535c9'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535ca'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535cb'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535cc'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535cd'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535ce'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535cf'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535d0'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535d1'), 'name': 'test'}, {'_id': ObjectId('5e92ec2dc4c667c3c12535d2'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535d4'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535d5'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535d6'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535d7'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535d8'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535d9'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535da'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535db'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535dc'), 'name': 'test'}, {'_id': ObjectId('5e92ec5ac4c667c3c12535dd'), 'name': 'test'}]