37创客科创中心

 找回密码
 立即注册
查看: 1796|回复: 0

mgex案例分解

[复制链接]

194

主题

324

帖子

2405

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2405
发表于 2023-6-12 01:54:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
mgdata.py

  1. url = "mongodb://root:37ck2020@iot.37ck.cn:27017"
  2. dbname = "曾老师"
  3. coll = "37ck"
  4. udb = "coll"

  5. update_old = {'content': '37ck而喝水可以满'}
  6. update_new = {'$set': {'content': '37ck而喝水可以使人精神饱满'}}

  7. data = [
  8.     {
  9.         # "_id": "8",
  10.         "articleid": "100001",
  11.         "content": "吃饭千万不能吃多了,因为会饱",
  12.         "userid": "1008",
  13.         "nickname": "天涯若比邻",

  14.     },
  15.     {
  16.         # "_id": "9",
  17.         "articleid": "100001",
  18.         "content": "吃饭前,先喝杯水或一碗汤,可减少饭量,对控制体重有帮助",
  19.         "userid": "1008",
  20.         "nickname": "玛丽莲·梦露",
  21.         "age": "18",
  22.         "phone": "13937165554",
  23.         "createdatetime": "new Date()",
  24.         "likenum": "8888",
  25.         "state": "null",
  26.     },
  27.     {"content": "脱水会使人精疲力竭,而喝水可以使人精神饱满"}
  28. ]

  29. one = {'content': '脱水会使人精疲力竭,而喝水可以使人精神饱满'}
  30. # {"articleid": "100001"}
  31. # {'_id': '6485fe8b294f6a06bc038efb'}
  32. # {"content": "脱水会使人精疲力竭,而喝水可以使人精神饱满"}

  33. # {"nickname": "咫尺天涯间"}

  34. newDoc = {
  35.     # "_id": "7",
  36.     "articleid": "100001",
  37.     "content": "脱水会使人精疲力竭,而喝水可以使人精神饱满",
  38.     "userid": "1007",
  39.     "nickname": "咫尺天涯间",
  40.     "age": "25",
  41.     "phone": "13937165554",
  42.     "createdatetime": "new Date()",
  43.     "likenum": "999",
  44.     "state": "1",
  45. }
复制代码



mgex.py
  1. # pip install pymongo==3.12.0
  2. from pymongo import MongoClient
  3. from mgdata import *


  4. class Mgdbex:
  5.     # 创建类的构造函数或初始化方法,其中包含一个参数self,表示类的示例,self在定义类的方法时是必须要有的,在带哦用时可以不传入相应参数
  6.     def __init__(self):
  7.         # 获取数据库的连接
  8.         # self.client = MongoClient('127.0.0.1', 27017)#本地数据库
  9.         self.url = url  # "mongodb://root:37ck2020@iot.37ck.cn:27017"
  10.         self.client = MongoClient(self.url)  # 远程数据库
  11.         self.dbname = dbname
  12.         print(self.client)

  13.     def getDBs(self):
  14.         '''获取所有数据库名字'''
  15.         dbs = self.client.list_database_names()
  16.         for db in dbs:
  17.             print("数据库列表:" + db)
  18.         return dbs

  19.     def getColl(self):
  20.         '''获取数据库中所有集合:数据表 名字'''
  21.         articledb = self.client[self.dbname]
  22.         collections = articledb.list_collection_names()
  23.         for collection in collections:
  24.             print(collection)

  25.     def createColl(self, col):
  26.         '''创建数据集:数据表'''
  27.         articledb = self.client[self.dbname]
  28.         articledb.create_collection(col)

  29.     def dropColl(self, col):
  30.         '''删除数据集'''
  31.         articledb = self.client[self.dbname]
  32.         articledb.drop_collection(col)

  33.     def insertOne(self, col):
  34.         '''0611插入单条数据:格式以字典(键值对)mgdata的newDoc;col就是数据表也是数据集合'''
  35.         self.articledb = self.client[self.dbname]
  36.         comment = self.articledb[col]
  37.         comment.insert_one(newDoc)

  38.     def insert(self, col):
  39.         '''0611插入多条数据insert_many:data是列表包含字典的json'''
  40.         self.articledb = self.client[self.dbname]
  41.         comment = self.articledb[col]
  42.         re = comment.insert_many(data)
  43.         print(re)

  44.     def find(self, col):
  45.         '''0611查找数据集所有数据'''
  46.         self.articledb = self.client[self.dbname]
  47.         comment = self.articledb[col]
  48.         documents = comment.find()
  49.         for document in documents:
  50.             print(document)

  51.     def updateDoc(self, col):
  52.         self.articledb = self.client[self.dbname]
  53.         comment = self.articledb[col]
  54.         comment.update_one(update_old,
  55.                            update_new)

  56.     def deleteDoc(self, col):
  57.         self.articledb = self.client[self.dbname]
  58.         comment = self.articledb[col]
  59.         comment.delete_one(one)


  60. # 主程序入口
  61. if __name__ == '__main__':
  62.     test = Mgdbex()  # 创建类的实例对象
  63.     # 0611
  64.     test.getDBs()
  65.     test.dbname = '0611'
  66.     # test.insert("0611")
  67.     # test.insertOne('0611')
  68.     # test.find("0611")
  69.     test.getColl()
复制代码



appmgex.py
  1. from mgex import Mgdbex

  2. Mg = Mgdbex()
  3. # Mg.getDBs()
  4. Mg.dbname = '0611'
  5. # 已经存在的时候创建会错误
  6. # Mg.createColl('37创')
  7. # Mg.dropColl('37创')
  8. # Mg.getColl()
  9. # Mg.insertOne('user')
  10. # Mg.insert('user')
  11. Mg.updateDoc('user')
  12. # Mg.deleteDoc('user')
  13. Mg.find('user')
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-13 08:15 , Processed in 0.138269 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表