|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
mgdata.py
- url = "mongodb://root:37ck2020@iot.37ck.cn:27017"
- dbname = "曾老师"
- coll = "37ck"
- udb = "coll"
- update_old = {'content': '37ck而喝水可以满'}
- update_new = {'$set': {'content': '37ck而喝水可以使人精神饱满'}}
- data = [
- {
- # "_id": "8",
- "articleid": "100001",
- "content": "吃饭千万不能吃多了,因为会饱",
- "userid": "1008",
- "nickname": "天涯若比邻",
- },
- {
- # "_id": "9",
- "articleid": "100001",
- "content": "吃饭前,先喝杯水或一碗汤,可减少饭量,对控制体重有帮助",
- "userid": "1008",
- "nickname": "玛丽莲·梦露",
- "age": "18",
- "phone": "13937165554",
- "createdatetime": "new Date()",
- "likenum": "8888",
- "state": "null",
- },
- {"content": "脱水会使人精疲力竭,而喝水可以使人精神饱满"}
- ]
- one = {'content': '脱水会使人精疲力竭,而喝水可以使人精神饱满'}
- # {"articleid": "100001"}
- # {'_id': '6485fe8b294f6a06bc038efb'}
- # {"content": "脱水会使人精疲力竭,而喝水可以使人精神饱满"}
- # {"nickname": "咫尺天涯间"}
- newDoc = {
- # "_id": "7",
- "articleid": "100001",
- "content": "脱水会使人精疲力竭,而喝水可以使人精神饱满",
- "userid": "1007",
- "nickname": "咫尺天涯间",
- "age": "25",
- "phone": "13937165554",
- "createdatetime": "new Date()",
- "likenum": "999",
- "state": "1",
- }
复制代码
mgex.py
- # pip install pymongo==3.12.0
- from pymongo import MongoClient
- from mgdata import *
- class Mgdbex:
- # 创建类的构造函数或初始化方法,其中包含一个参数self,表示类的示例,self在定义类的方法时是必须要有的,在带哦用时可以不传入相应参数
- def __init__(self):
- # 获取数据库的连接
- # self.client = MongoClient('127.0.0.1', 27017)#本地数据库
- self.url = url # "mongodb://root:37ck2020@iot.37ck.cn:27017"
- self.client = MongoClient(self.url) # 远程数据库
- self.dbname = dbname
- print(self.client)
- def getDBs(self):
- '''获取所有数据库名字'''
- dbs = self.client.list_database_names()
- for db in dbs:
- print("数据库列表:" + db)
- return dbs
- def getColl(self):
- '''获取数据库中所有集合:数据表 名字'''
- articledb = self.client[self.dbname]
- collections = articledb.list_collection_names()
- for collection in collections:
- print(collection)
- def createColl(self, col):
- '''创建数据集:数据表'''
- articledb = self.client[self.dbname]
- articledb.create_collection(col)
- def dropColl(self, col):
- '''删除数据集'''
- articledb = self.client[self.dbname]
- articledb.drop_collection(col)
- def insertOne(self, col):
- '''0611插入单条数据:格式以字典(键值对)mgdata的newDoc;col就是数据表也是数据集合'''
- self.articledb = self.client[self.dbname]
- comment = self.articledb[col]
- comment.insert_one(newDoc)
- def insert(self, col):
- '''0611插入多条数据insert_many:data是列表包含字典的json'''
- self.articledb = self.client[self.dbname]
- comment = self.articledb[col]
- re = comment.insert_many(data)
- print(re)
- def find(self, col):
- '''0611查找数据集所有数据'''
- self.articledb = self.client[self.dbname]
- comment = self.articledb[col]
- documents = comment.find()
- for document in documents:
- print(document)
- def updateDoc(self, col):
- self.articledb = self.client[self.dbname]
- comment = self.articledb[col]
- comment.update_one(update_old,
- update_new)
- def deleteDoc(self, col):
- self.articledb = self.client[self.dbname]
- comment = self.articledb[col]
- comment.delete_one(one)
- # 主程序入口
- if __name__ == '__main__':
- test = Mgdbex() # 创建类的实例对象
- # 0611
- test.getDBs()
- test.dbname = '0611'
- # test.insert("0611")
- # test.insertOne('0611')
- # test.find("0611")
- test.getColl()
复制代码
appmgex.py
- from mgex import Mgdbex
- Mg = Mgdbex()
- # Mg.getDBs()
- Mg.dbname = '0611'
- # 已经存在的时候创建会错误
- # Mg.createColl('37创')
- # Mg.dropColl('37创')
- # Mg.getColl()
- # Mg.insertOne('user')
- # Mg.insert('user')
- Mg.updateDoc('user')
- # Mg.deleteDoc('user')
- Mg.find('user')
复制代码
|
|