实验一 新建mogodb的库
import pymongo
from pymongo import MongoClient
# client=MongoClient("localhost",27017) #和下面一行二选一
client = MongoClient("mongodb://localhost:27017/")
for i in range(2,30):#操作数据库
db=client["ccf1"]#新建集合
db["ccf23"].insert({"KEasYS":2874435})#集合中建立关系表
dblist = client.list_database_names()#数据集合名字
if "ccf1" in dblist:
print("数据库已存在!")
client.close
实验二 插入集合
2.1插入一个数据
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
mydict = { "name": "WV", "AREA": "嘉定"}#支持中文
x = mycol.insert_one(mydict) #返回这个数据的id,id是全局唯一变量
print(x)
2.2插入多个数据
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
mylist = [
{ "name": "WV", "AREA": "中国"},
{ "name": "WV", "AREA": "SH"},
{ "name": "WV", "AREA": "安亭"},
{ "_id":"12345","name": "WV", "AREA": "待定","投资":"1000W"}#指定id,可以是字符串也可以是数字;存储的数据可以结构不一样
]
x = mycol.insert_many(mylist)
print(x.inserted_ids)
实验三 数据检索
3.1检索单个数据
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
mylist = [
{ "name": "WV", "AREA": "中国"},
{ "name": "WV", "AREA": "SH"},
{ "name": "WV", "AREA": "安亭"},
{ "name": "WV","part":"荣威 RX5", "AREA": "待定","投资":"1000W"}
]
x = mycol.insert_many(mylist)
print(x.inserted_ids)
print(mycol.find_one())#本数据库的第一个数据
3.2检索所有数据
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
mylist = [
{ "name": "WV", "AREA": "中国"},
{ "name": "WV", "AREA": "SH"},
{ "name": "WV", "AREA": "安亭"},
{ "name": "WV","part":"荣威 RX5", "AREA": "待定","投资":"1000W"}
]
x = mycol.insert_many(mylist)
print(x.inserted_ids)
for i in mycol.find():#打出所有数据
print(i)
print(i["name"])#用关键字打出对应的键值
3.3检索指定字段的数据
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
mylist = [
{ "name": "WV", "AREA": "中国"},
{ "name": "WV", "AREA": "SH"},
{ "name": "WV", "AREA": "安亭"},
{ "name": "WVg","part":"荣威 W5", "AREA": "待定","投资":"1000W"}
]
x = mycol.insert_many(mylist)
for x in mycol.find({"part":{"$regex": "R"}}):#加入正则表达式
print(x)
for x in mycol.find({"part":"荣威"}):#加入限定条件,只要part中包括荣威就会被列出
print(x)
for x in mycol.find({},{ "_id": 0, "part": 1, "投资": 1 }):#检测关键字存在
print(x)
myresult = mycol.find().limit(3)#反馈前三条
for x in myresult:
print(x)
实验四 改数据
4.1修改所有匹配到的记录
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
myquery = { "name": "WV","part":"荣威 RX5" }#检索筛选条件
newvalues = { "$set": { "AREA": "嘉定" } }#替换的数据
x = mycol.update_many(myquery, newvalues)#替换触发
4.2修改单条记录
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
myquery = { "name": "WV","part":"荣威 RX5" }#检索筛选条件
newvalues = { "$set": { "AREA": "扬州" } }#替换的数据
mycol.update_one(myquery, newvalues)#替换触发,只修改遇到的第一条数据
# 输出修改后的 "sites" 集合
for x in mycol.find():
print(x)
实验五 排序
5.1
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
mydoc = mycol.find().sort("part",-1)#按照part的值倒叙排序,值相同的比较_id
for x in mydoc:
print(x)
实验六 删除数据
6.1删除单个字段
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
myquery = { "AREA": "anting" }
mycol.delete_one(myquery)
for x in mycol.find():
print(x)
6.2删除所有字段
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
myquery = { "AREA": "anting" }
mycol.delete_many(myquery)
for x in mycol.find():
print(x)
6.3删除集合
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["saic"]
mycol = mydb["sites"]
mycol.drop()
转载于:https://blog.51cto.com/14156081/2348591