1 Star 0 Fork 0

吴超/huamei

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MongoDbHandler.py 2.74 KB
一键复制 编辑 原始数据 按行查看 历史
wmc 提交于 2019-10-20 22:27 . first commit
# coding=utf-8
from pymongo import MongoClient
# import pandas as pd
import time
class MongoDbHandler(object):
def __init__(self, ip='127.0.0.1', user='admin', keyword='admin'):
if user == None or keyword == None:
URL = 'mongodb://{0}'.format(ip)
else:
URL = 'mongodb://{0}:{1}@{2}'.format(user, keyword, ip)
# print(user,keyword)
print(URL)
self.__Client = MongoClient(URL)
def find_all(self, db, collection, condition = None,no_id={'_id':0}):
self.__db = self.__Client[db]
self.__collection = self.__db[collection]
cursor = self.__collection.find(condition,no_id)
result = []
for contentDict in cursor:
result.append(contentDict)
# print(result)
return result
def find_all_cursor(self, db, collection, condition = None):
self.__db = self.__Client[db]
self.__collection = self.__db[collection]
cursor = self.__collection.find(condition)
return cursor
def insert_one(self, db, collection, jsonData):
_db = self.__Client[db]
_collection = _db[collection]
result = _collection.insert_one(jsonData)
return result
def insert_many(self, db, collection, jsonData):
_db = self.__Client[db]
_collection = _db[collection]
result = _collection.insert_many(jsonData)
return result
def update_many(self,db,collection,old_json,new_json):
_db = self.__Client[db]
_collection = _db[collection]
result = _collection.update_many(old_json,new_json)
return result
def delete_many(self, db, collection, jsonData):
_db = self.__Client[db]
_collection = _db[collection]
result = _collection.delete_many(jsonData)
return result
def drop(self,db,collection):
_db = self.__Client[db]
_collection = _db[collection]
_collection.drop()
def close(self):
return self.__Client.close()
if __name__ == "__main__":
# from MongoDbHandler import MongoDbHandler
# from pandas import DataFrame
# finallyResult = {}
# finallyResult["aa"]="bb"
# finallyResult["cc"]="dd"
# df = DataFrame(finallyResult,index=[1])
# data = df.to_dict('records')
mongoSession = MongoDbHandler()#A为mongodb的name,B为用户名,C为密码
# mongoSession = MongoDbHandler('127.0.0.1','admin', 'admin')#A为mongodb的name,B为用户名,C为密码
# mongoSession.insert_many("jk", "E", re_dict)#D为数据库名,E为集合名
qw=mongoSession.find_all("jk", "col")#D为数据库名,E为集合名
# print(111111111111)
print(qw,type(qw),len(qw))
# print(22222222222222)
# time.sleep(2)
# mongoSession.close()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wumuchen/huamei.git
git@gitee.com:wumuchen/huamei.git
wumuchen
huamei
huamei
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385