37创客科创中心

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

0611 mgdata

[复制链接]

45

主题

84

帖子

909

积分

版主

Rank: 7Rank: 7Rank: 7

积分
909
发表于 2023-6-11 09:37:02 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 37ck 于 2023-6-11 09:39 编辑

mgdata.py
  1. url = "mongodb://root:37ck2020@iot.37ck.cn:27017"
  2. dbname = "0611"
  3. coll = "37ck"
  4. udb="coll"

  5. update_data=[{"content": "脱水会使人精疲力竭,而喝水可以使人精神饱满"},
  6.                            {"$set": {"content": "吃饭前,先喝杯水或一碗汤,可减少饭量,对控制体重有明显的帮助"}}]
  7. data = [
  8.     {
  9.         # "_id": "8",
  10.         "articleid": "100001",
  11.         "content": "吃饭千万不能吃多了,因为会饱",
  12.         "userid": "1008",
  13.         "nickname": "天涯若比邻",
  14.         "age": "21",
  15.         "phone": "135846240285",
  16.         "createdatetime": "new Date()",
  17.         "likenum": "666",
  18.         "state": "1",
  19.     },
  20.     {
  21.         # "_id": "9",
  22.         "articleid": "100001",
  23.         "content": "吃饭前,先喝杯水或一碗汤,可减少饭量,对控制体重有帮助",
  24.         "userid": "1008",
  25.         "nickname": "玛丽莲·梦露",
  26.         "age": "18",
  27.         "phone": "13937165554",
  28.         "createdatetime": "new Date()",
  29.         "likenum": "8888",
  30.         "state": "null",
  31.     }
  32. ]

  33. newDoc = {
  34.     # "_id": "7",
  35.     "articleid": "100001",
  36.     "content": "脱水会使人精疲力竭,而喝水可以使人精神饱满",
  37.     "userid": "1007",
  38.     "nickname": "咫尺天涯间",
  39.     "age": "25",
  40.     "phone": "13937165554",
  41.     "createdatetime": "new Date()",
  42.     "likenum": "999",
  43.     "state": "1",
  44. }
复制代码
appmgex.py
  1. # pip install pymongo==3.12.0
  2. from pymongo import MongoClient
  3. from mgdata import data,newDoc,url,coll

  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 = "37ck0604"
  12.         print(self.client)
  13.         dbs = self.client.list_database_names()
  14.         for db in dbs:
  15.             print("数据库列表:" + db)

  16.     def getDBs(self):
  17.         dbs = self.client.list_database_names()
  18.         for db in dbs:
  19.             print(db)

  20.     def getColl(self):
  21.         articledb = self.client["articledb"]
  22.         collections = articledb.list_collection_names()
  23.         for collection in collections:
  24.             print(collection)

  25.     def createColl(self):
  26.         articledb = self.client["articledb"]
  27.         articledb.create_collection("itcast")

  28.     def dropColl(self):
  29.         articledb = self.client["articledb"]
  30.         articledb.drop_collection("itcast")

  31.     def findDoc(self):
  32.         self.articledb = self.client["articledb"]
  33.         comment = self.articledb["comment"]
  34.         documents = comment.find()
  35.         for document in documents:
  36.             print(document)

  37.     def insertOneDoc(self):
  38.         self.articledb = self.client["articledb"]
  39.         comment = self.articledb["comment"]
  40.         comment.insert_one(newDoc)

  41.     def find(self, col):
  42.         self.articledb = self.client[self.dbname]
  43.         comment = self.articledb[col]
  44.         documents = comment.find()
  45.         for document in documents:
  46.             print(document)

  47.     def insert(self, col):
  48.         self.articledb = self.client[self.dbname]
  49.         comment = self.articledb[col]
  50.         re = comment.insert_many(data)
  51.         print(re)

  52.     def insertManyDoc(self):
  53.         self.articledb = self.client["articledb"]
  54.         comment = self.articledb["comment"]
  55.         re = comment.insert_many(data)
  56.         print(re)

  57.     def updateDoc(self):
  58.         self.articledb = self.client["articledb"]
  59.         comment = self.articledb["comment"]
  60.         comment.update_one({"content": "脱水会使人精疲力竭,而喝水可以使人精神饱满"},
  61.                            {"$set": {"content": "吃饭前,先喝杯水或一碗汤,可减少饭量,对控制体重有明显的帮助"}})

  62.     def deleteDoc(self):
  63.         self.articledb = self.articledb = self.client["articledb"]
  64.         comment = self.articledb["comment"]
  65.         comment.delete_one({"nickname": "咫尺天涯间"})

  66. # 主程序入口
  67. if __name__ == '__main__':
  68.     # 创建类的实例对象
  69.     test = Mgdbex()
  70.     test.dbname='0611'
  71.     test.insert("37ck")
  72.     test.find("37ck")
  73.     # test.getDBs()
  74.     # test.createColl()
  75.     # test.dropColl()
  76.     # test.getColl()
  77.     # test.insertOneDoc()
  78.     # test.insertManyDoc()
  79.     # test.updateDoc()
  80.     # test.deleteDoc()
  81.     # test.findDoc()
复制代码


回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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