- 建立连接
- 查询某一条数据
- 更新某一条数据 {"$set":newvalue}
- 批量存储
# encoding: utf-8
"""
@author: julse@qq.com
@time: 2020/11/25 14:58
@desc:
"""
import pymongo
from pymongo import MongoClient
class DataOperation:
def __init__(self, firstname,secondname, address="XXX", port=XXX):
self.firstname = firstname
self.secondname = secondname
self.address = address
self.port = port
self.collection = self.Connected()
def Connected(self):
client = pymongo.MongoClient(self.address, self.port)
db = client['dbname']
db.authenticate("username", "password")
return client[self.firstname][self.secondname]
def UpdateOne(self,oldvalue, newvalue):
newvalue = {"$set":newvalue}
self.collection.update_one(oldvalue, newvalue)
def QueryOne(self, key, value):
dic = {}
dic[key] = value
print(str(dic))
return self.collection.find_one(dic)
def StorageIter(self, iter):
for i in iter:
self.collection.insert(i)
使用
do = DataOperation(firstname, secondname, address="XXX", port=XXX)
projection = {'key_XXX':True,'_id':'False}
target_name = 'XXX'
accessions = do.Query(key, target_name, projection=projection)