37创客科创中心

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

文田0611:mongodb基本操作

[复制链接]

45

主题

84

帖子

909

积分

版主

Rank: 7Rank: 7Rank: 7

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

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

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

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

mgdata.py

  1. url = "mongodb://root:37ck2020@iot.37ck.cn:27017"
  2. dbname = "曾老师"
  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.     },
  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. ]

  28. newDoc = {
  29.     # "_id": "7",
  30.     "articleid": "100001",
  31.     "content": "脱水会使人精疲力竭,而喝水可以使人精神饱满",
  32.     "userid": "1007",
  33.     "nickname": "咫尺天涯间",
  34.     "age": "25",
  35.     "phone": "13937165554",
  36.     "createdatetime": "new Date()",
  37.     "likenum": "999",
  38.     "state": "1",
  39. }
复制代码
appmgex.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.         # 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 insertOne(self, col):
  38.         '''插入单条数据:格式以字典(键值对)mgdata的newDoc;col就是数据表也是数据集合'''
  39.         self.articledb = self.client[self.dbname]
  40.         comment = self.articledb[col]
  41.         comment.insert_one(newDoc)

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

  48.     def find(self, col):
  49.         self.articledb = self.client[self.dbname]
  50.         comment = self.articledb[col]
  51.         documents = comment.find()
  52.         for document in documents:
  53.             print(document)

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

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

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


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




回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-13 10:05 , Processed in 0.159180 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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