{"id":747,"date":"2019-05-01T17:56:27","date_gmt":"2019-05-01T09:56:27","guid":{"rendered":"http:\/\/www.eait.co\/?p=747"},"modified":"2022-01-12T10:03:03","modified_gmt":"2022-01-12T02:03:03","slug":"class-mongodb","status":"publish","type":"post","link":"https:\/\/notes.coremix.net\/?p=747","title":{"rendered":"class mongoDB"},"content":{"rendered":"<div># import pymongo<\/div>\n<div># class MyMongodb:<\/div>\n<div># def __init__(self,dataname,collection):<\/div>\n<div># client = pymongo.MongoClient() # \u5efa\u7acb\u8fde\u63a5<\/div>\n<div># self.db = client[dataname] # \u6307\u5b9a\u6570\u636e\u5e93\u540d<\/div>\n<div># self.usecollection = db[collection]<\/div>\n<div>#<\/div>\n<div># def find(self,data,onlyOne = True):<\/div>\n<div>#<\/div>\n<div>#<\/div>\n<div># def insert(self,data,onlyOne = True):<\/div>\n<div><\/div>\n<div><\/div>\n<div>import pymongo<\/div>\n<div>class MyMong:<\/div>\n<div>def __init__(self,database,collection):<\/div>\n<div>self.client = pymongo. MongoClient()#\u5efa\u7acb\u8fde\u63a5<\/div>\n<div>self.db = self.client[database]#\u8fde\u63a5\u6570\u636e\u5e93<\/div>\n<div>self.my_col = self.db[collection]<\/div>\n<div>def find(self,onlyOne = True):<\/div>\n<div>if onlyOne:<\/div>\n<div>ret = self.my_col.find_one()<\/div>\n<div>return ret<\/div>\n<div>else:<\/div>\n<div>ret = self.my_col.find()<\/div>\n<div>for i in ret:<\/div>\n<div>print(i)<\/div>\n<div>def insert(self,data,onlyOne = True):<\/div>\n<div>if onlyOne:<\/div>\n<div>self.my_col.insert_one(data)<\/div>\n<div>else:<\/div>\n<div>self.my_col.insert_many(data)<\/div>\n<div>def update(self,data,new_data,onlyOne = True):<\/div>\n<div>if onlyOne:<\/div>\n<div>self.my_col.update_one(data,{&#8216;$set&#8217;:new_data})<\/div>\n<div>else:<\/div>\n<div>self.my_col.update_many(data,{&#8216;$set&#8217;:new_data})<\/div>\n<div>def delete(self, data, onlyOne=True):<\/div>\n<div>if onlyOne:<\/div>\n<div>self.my_col.delete_one(data)<\/div>\n<div>else:<\/div>\n<div>self.my_col.delete(data)<\/div>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p># import pymongo # class MyMongodb: # def __init__(self [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[20],"class_list":["post-747","post","type-post","status-publish","format-standard","hentry","category-python_note","tag-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=747"}],"version-history":[{"count":3,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":998,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=\/wp\/v2\/posts\/747\/revisions\/998"}],"wp:attachment":[{"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.coremix.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}