37创客科创中心

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

python mongodb案例

[复制链接]

45

主题

84

帖子

909

积分

版主

Rank: 7Rank: 7Rank: 7

积分
909
发表于 2023-6-4 19:26:18 | 显示全部楼层 |阅读模式

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

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

x
  1. # pip install pymongo==3.12.0
  2. from pymongo import MongoClient

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


  29. class Test:
  30.         # 创建类的狗仔函数或初始化方法,其中包含一个参数self,表示类的示例,self在定义类的方法时是必须要有的,在带哦用时可以不传入相应参数
  31.         def __init__(self):
  32.                 # 获取数据库的连接
  33.                 # self.client = MongoClient('127.0.0.1', 27017)#本地数据库
  34.                 self.client = MongoClient("mongodb://root:37ck2020@iot.37ck.cn:27017")#远程数据库
  35.                 self.dbname = "37ck0604"
  36.                 print(self.client)
  37.                 dbs = self.client.list_database_names()
  38.                 for db in dbs:
  39.                         print("数据库列表:" + db)
  40.        
  41.         def getDBs(self):
  42.                 dbs = self.client.list_database_names()
  43.                 for db in dbs:
  44.                         print(db)
  45.        
  46.         def getColl(self):
  47.                 articledb = self.client["articledb"]
  48.                 collections = articledb.list_collection_names()
  49.                 for collection in collections:
  50.                         print(collection)
  51.        
  52.         def createColl(self):
  53.                 articledb = self.client["articledb"]
  54.                 articledb.create_collection("itcast")
  55.        
  56.         def dropColl(self):
  57.                 articledb = self.client["articledb"]
  58.                 articledb.drop_collection("itcast")
  59.        
  60.         def findDoc(self):
  61.                 self.articledb = self.client["articledb"]
  62.                 comment = self.articledb["comment"]
  63.                 documents = comment.find()
  64.                 for document in documents:
  65.                         print(document)
  66.        
  67.         def insertOneDoc(self):
  68.                 self.articledb = self.client["articledb"]
  69.                 comment = self.articledb["comment"]
  70.                 newDoc = {
  71.                         # "_id": "7",
  72.                         "articleid": "100001",
  73.                         "content": "脱水会使人精疲力竭,而喝水可以使人精神饱满",
  74.                         "userid": "1007",
  75.                         "nickname": "咫尺天涯间",
  76.                         "age": "25",
  77.                         "phone": "13937165554",
  78.                         "createdatetime": "new Date()",
  79.                         "likenum": "999",
  80.                         "state": "1",
  81.                 }
  82.                 comment.insert_one(newDoc)
  83.        
  84.         def find(self,col):
  85.                 self.articledb = self.client[self.dbname]
  86.                 comment = self.articledb[col]
  87.                 documents = comment.find()
  88.                 for document in documents:
  89.                         print(document)
  90.        
  91.         def insert(self,col):
  92.                 self.articledb = self.client[self.dbname]
  93.                 comment = self.articledb[col]
  94.                 re = comment.insert_many(data)
  95.                 print(re)
  96.        
  97.         def insertManyDoc(self):
  98.                 self.articledb = self.client["articledb"]
  99.                 comment = self.articledb["comment"]
  100.                 newDocs = [
  101.                         {
  102.                                 # "_id": "8",
  103.                                 "articleid": "100001",
  104.                                 "content": "吃饭千万不能吃多了,因为会饱",
  105.                                 "userid": "1008",
  106.                                 "nickname": "天涯若比邻",
  107.                                 "age": "21",
  108.                                 "phone": "135846240285",
  109.                                 "createdatetime": "new Date()",
  110.                                 "likenum": "666",
  111.                                 "state": "1",
  112.                         },
  113.                         {
  114.                                 # "_id": "9",
  115.                                 "articleid": "100001",
  116.                                 "content": "吃饭前,先喝杯水或一碗汤,可减少饭量,对控制体重有帮助",
  117.                                 "userid": "1008",
  118.                                 "nickname": "玛丽莲·梦露",
  119.                                 "age": "18",
  120.                                 "phone": "13937165554",
  121.                                 "createdatetime": "new Date()",
  122.                                 "likenum": "8888",
  123.                                 "state": "null",
  124.                         }
  125.                 ]
  126.                 re = comment.insert_many(newDocs)
  127.                 print(re)
  128.        
  129.         def updateDoc(self):
  130.                 self.articledb = self.client["articledb"]
  131.                 comment = self.articledb["comment"]
  132.                 comment.update_one({"content": "脱水会使人精疲力竭,而喝水可以使人精神饱满"},
  133.                                    {"$set": {"content": "吃饭前,先喝杯水或一碗汤,可减少饭量,对控制体重有明显的帮助"}})
  134.        
  135.         def deleteDoc(self):
  136.                 self.articledb = self.articledb = self.client["articledb"]
  137.                 comment = self.articledb["comment"]
  138.                 comment.delete_one({"nickname": "咫尺天涯间"})


  139. # 主程序入口
  140. if __name__ == '__main__':
  141.         # 创建类的实例对象
  142.         test = Test()
  143.         test.insert("user")
  144.         test.find("user")
  145.         # test.getDBs()
  146.         # test.createColl()
  147.         # test.dropColl()
  148.         # test.getColl()
  149.         # test.insertOneDoc()
  150.         # test.insertManyDoc()
  151.         # test.updateDoc()
  152.         # test.deleteDoc()
  153.         # test.findDoc()
复制代码


回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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