代码拉取完成,页面将自动刷新
# 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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。