活动地址:CSDN21天学习挑战赛
目录
一、安装MongoDB数据库
pip install pymongo
二、MongoDB数据操纵
1、连接MongoDB
from pymongo import MongoClient
def mongodb_init01():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
print(client)
def mongodb_init02():
#mongodb://{}:{} .format方式
uri = "mongodb://{}:{}".format('127.0.0.1', 27017)
client = MongoClient(uri)
print(client)
def mongodb_init03():
#mongodb://host:prot方式
client = MongoClient('mongodb://127.0.0.1:27017/')
print(client)
if __name__ == '__main__':
mongodb_init01()
mongodb_init02()
mongodb_init03()
2、数据库操作
注意:
database_names、collection_names 在Python3.7之后改为list_xxx
数据库和集合只有插入数据才会创建
1)插入单条数据
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 查询所有数据库
database_list = client.list_database_names()
print(database_list)
# 判断数据库是否存在
if 'test001' in database_list:
print("数据库存在")
else:
print('数据库不存在')
# 使用当前数据库 use database一样
test_database = client.test001
print(test_database)
# 创建新的数据库
new_database = client.test001
print(new_database)
new_collection = new_database.test001
new_data = new_collection.insert_one({"name": "天天", "age": 22})
print(new_data)
# 删除数据
# client.drop_database('test001')
if __name__ == '__main__':
mongodb_init()
- 在MongoDB shell里面操作
2)插入多条数据
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
#创建新的集合
new_collection = new_database.test001
data = [
{"name": "张飞", "hometown": "蜀国", "age": 30, "sex": "男"},
{"name": "关羽", "hometown": "蜀国", "age": 40, "sex": "男"},
{"name": "刘备", "hometown": "蜀国", "age": 50, "sex": "男"},
{"name": "曹操", "hometown": "魏国", "age": 45, "sex": "男"},
{"name": "司马懿", "hometown": "魏国", "age": 45, "sex": "男"},
{"name": "孙权", "hometown": "吴国", "age": 50, "sex": "男"},
{"name": "貂蝉", "hometown": "未知", "age": 18, "sex": "女"},
{"name": "西施", "hometown": "越国", "age": 18, "sex": "女"},
{"name": "王昭君", "hometown": "西汉", "age": 18, "sex": "女"},
{"name": "杨玉环", "hometown": "唐朝", "age": 18, "sex": "女"}
]
# 插入多条数据
data = new_collection.insert_many(data)
print(data)
if __name__ == '__main__':
mongodb_init()
结果:
{ "_id" : ObjectId("62f321c0c8896586fed57929"), "name" : "张飞", "hometown" : "蜀国", "age" : 30, "sex" : "男" }
{ "_id" : ObjectId("62f321c0c8896586fed5792a"), "name" : "关羽", "hometown" : "蜀国", "age" : 40, "sex" : "男" }
{ "_id" : ObjectId("62f321c0c8896586fed5792b"), "name" : "刘备", "hometown" : "蜀国", "age" : 50, "sex" : "男" }
{ "_id" : ObjectId("62f321c0c8896586fed5792c"), "name" : "曹操", "hometown" : "魏国", "age" : 45, "sex" : "男" }
{ "_id" : ObjectId("62f321c0c8896586fed5792d"), "name" : "司马懿", "hometown" : "魏国", "age" : 45, "sex" : "男" }
{ "_id" : ObjectId("62f321c0c8896586fed5792e"), "name" : "孙权", "hometown" : "吴国", "age" : 50, "sex" : "男" }
{ "_id" : ObjectId("62f321c0c8896586fed5792f"), "name" : "貂蝉", "hometown" : "未知", "age" : 18, "sex" : "女" }
{ "_id" : ObjectId("62f321c0c8896586fed57930"), "name" : "西施", "hometown" : "越国", "age" : 18, "sex" : "女" }
{ "_id" : ObjectId("62f321c0c8896586fed57931"), "name" : "王昭君", "hometown" : "西汉", "age" : 18, "sex" : "女" }
{ "_id" : ObjectId("62f321c0c8896586fed57932"), "name" : "杨玉环", "hometown" : "唐朝", "age" : 18, "sex" : "女" }
3)指定id插入数据
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
# 创建新的集合
new_collection = new_database.test001
data = [
{"_id": 1, "name": "张飞", "hometown": "蜀国", "age": 30, "sex": "男"},
{"_id": 2, "name": "关羽", "hometown": "蜀国", "age": 40, "sex": "男"},
{"_id": 3, "name": "刘备", "hometown": "蜀国", "age": 50, "sex": "男"},
{"_id": 4, "name": "曹操", "hometown": "魏国", "age": 45, "sex": "男"},
{"_id": 5, "name": "司马懿", "hometown": "魏国", "age": 45, "sex": "男"},
{"_id": 6, "name": "孙权", "hometown": "吴国", "age": 50, "sex": "男"},
{"_id": 7, "name": "貂蝉", "hometown": "未知", "age": 18, "sex": "女"},
{"_id": 8, "name": "西施", "hometown": "越国", "age": 18, "sex": "女"},
{"_id": 9, "name": "王昭君", "hometown": "西汉", "age": 18, "sex": "女"},
{"_id": 10, "name": "杨玉环", "hometown": "唐朝", "age": 18, "sex": "女"}
]
# 插入多条数据
data = new_collection.insert_many(data)
print(data)
if __name__ == '__main__':
mongodb_init()
结果:
{ "_id" : 1, "name" : "张飞", "hometown" : "蜀国", "age" : 30, "sex" : "男" }
{ "_id" : 2, "name" : "关羽", "hometown" : "蜀国", "age" : 40, "sex" : "男" }
{ "_id" : 3, "name" : "刘备", "hometown" : "蜀国", "age" : 50, "sex" : "男" }
{ "_id" : 4, "name" : "曹操", "hometown" : "魏国", "age" : 45, "sex" : "男" }
{ "_id" : 5, "name" : "司马懿", "hometown" : "魏国", "age" : 45, "sex" : "男" }
{ "_id" : 6, "name" : "孙权", "hometown" : "吴国", "age" : 50, "sex" : "男" }
{ "_id" : 7, "name" : "貂蝉", "hometown" : "未知", "age" : 18, "sex" : "女" }
{ "_id" : 8, "name" : "西施", "hometown" : "越国", "age" : 18, "sex" : "女" }
{ "_id" : 9, "name" : "王昭君", "hometown" : "西汉", "age" : 18, "sex" : "女" }
{ "_id" : 10, "name" : "杨玉环", "hometown" : "唐朝", "age" : 18, "sex" : "女" }
4)查询数据
- 普通查询
查询一条数据:集合.fond_one、
查询所有数据:集合.fond()
和SQL使用一样
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
# 创建新的集合
new_collection = new_database.test001
# 查询一条数据
i = new_collection.find_one()
print(i)
print('=' * 20)
# 查询所有数据
for i in new_collection.find():
print(i)
if __name__ == '__main__':
mongodb_init()
- 根据条件查询
‘_id’ = 0时(不显示当前数据) name、age、sex三个参数的值同时必须为0或1,三个参数为0数据取反,1时为显示
‘_id’ = 1时(显示当前数据) name、age、sex三个参数为0时数据取反,1时为显示
5)指定条件查询
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
# 创建新的集合
new_collection = new_database.test001
# 查询所有数据
for i in new_collection.find({'name': "天天", 'age': 22}):
print(i)
if __name__ == '__main__':
mongodb_init()
6)正则表达式查询
{“字段名(key)”: {“$regex”:“正则表达式条件”}}
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
# 创建新的集合
new_collection = new_database.test001
# 查询所有数据
for i in new_collection.find({"name": {"$regex": "^曹"}}):
print(i)
if __name__ == '__main__':
mongodb_init()
7)限定显示条数limit()
limit(限定整数)
8)修改数据
修改一条数据:update_one
修改多条数据:update_many --操作雷同
匹配多条数据也只会修改最优先查询到的一条数据
把age=22修改为25
注意:“ s e t ”运算符将字段的值替换为指定的值。如果该字段不存在,则“ set” 运算符将字段的值替换为指定的值。如果该字段不存在,则 “ set”运算符将字段的值替换为指定的值。如果该字段不存在,则“set” 将添加具有指定值的新字段,前提是新字段不违反类型约束。
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
# 创建新的集合
new_collection = new_database.test001
# 修改一条数据
old_data = {"name": "天天", "age": 22}
new_data = {"$set": {"name": "天天", "age": 25}}
one_data = new_collection.update_one(old_data, new_data)
for i in new_collection.find():
print(i)
if __name__ == '__main__':
mongodb_init()
9)删除数据
删除一条数据:delete_one
删除所有数据:delete_many --操作雷同
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
# 创建新的集合
new_collection = new_database.test001
# 修改一条数据
old_data = {"name": "天天", "age": 25}
new_collection.delete_one(old_data)
for i in new_collection.find():
print(i)
if __name__ == '__main__':
mongodb_init()
10)删除集合
drop()
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
# 创建新的集合
new_collection = new_database.test001
new_collection.drop()
if __name__ == '__main__':
mongodb_init()
结果:
> show tables
test001
> db.test001.find()
{ "_id" : ObjectId("62f336eff1da83d5762638c8"), "name" : "天天", "age" : 22 }
> db.test001.find()
> show tables
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
new_database 0.000GB
>
11)sort排序
默认是升序 或者指定1,-1是降序
- 升序
- 降序
from pymongo import MongoClient
def mongodb_init():
# host+port方式
client = MongoClient('127.0.0.1', port=27017)
# 创建新的数据库
new_database = client.test001
# 创建新的集合
new_collection = new_database.test001
for i in new_collection.find().sort("age",-1):
print(i)
if __name__ == '__main__':
mongodb_init()