python封装mongo函数-创新互联

class PyMongo(object):
    def save_mongo(self,data,collection_name)->bool:
        '''
            插入数据到Mongodb
            @:param data 插入
            @:param collection_name 要插入连接名字
        '''
        try:
            conn = MongoClient(MONGO_URL)
            db = conn[DB_NAME]
            collection = db[collection_name]
            if isinstance(data,dict):
                collection.insert_one(data)
            elif isinstance(data,list):
                collection.insert_many(data)
            conn.close()
            return True
        except Exception as ex:
            print(f"mongo链接异常:{ex}")
            return False

    def find_count(self,db_name,select_info):
        try:
            conn = MongoClient(MONGO_URL)
            db = conn[DB_NAME]
            collection = db[db_name]
            count = collection.count_documents(select_info)
            conn.close()
            return count
        except Exception as ex:
            print(ex)
            return False

    def find_mongo(self,db_name,select_dic,filer_dic=None):
      try:
          conn = MongoClient(MONGO_URL)
          db = conn[DB_NAME]
          collection = db[db_name]
          if select_dic:
              result = collection.find(select_dic)
          else:
              result = collection.find(select_dic,filer_dic)
          result_list = list(result)
          conn.close()
          return result_list

    def update_many(self,ip):
        try:
            conn = MongoClient(MONGO_URL)
            db = conn[DB_NAME]
            collection = db[COLLECTION_NAME]
            result = collection.update_many({"ip":ip}, {"$set": {"flag": 0}})
            conn.close()
            return True
        except Exception as ex:
            print(ex)
            return False

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

创新互联公司主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、自适应网站建设、程序开发、网站优化、微网站、微信小程序等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站建设、做网站、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。
分享标题:python封装mongo函数-创新互联
文章路径:http://pwwzsj.com/article/ipgdg.html