Python pymongo.MongoClient() Examples
The following are 30
code examples of pymongo.MongoClient().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pymongo
, or try the search function
.
Example #1
Source File: db.py From pipelinewise with Apache License 2.0 | 7 votes |
def get_mongodb_connection(host: str, port: Union[str, int], user: str, password: str, database: str, auth_database: str)->Database: """ Creates a mongoDB connection to the db to sync from Returns: Database instance with established connection """ return pymongo.MongoClient(host=host, port=int(port), username=user, password=password, authSource=auth_database)[database]
Example #2
Source File: crits.py From ACE with Apache License 2.0 | 7 votes |
def update_status(crits_id, status): assert isinstance(crits_id, str) assert status in [ 'Informational', 'Analyzed' ] # API support for changing the status message was not implemented at the time I wrote this # download the crits indicator JSON directly from the crits mongo database client = MongoClient(saq.CONFIG['crits']['mongodb_uri']) db = client['crits'] collection = db['indicators'] logging.debug("updating status of {} to {}".format(crits_id, status)) result = collection.update({'_id': ObjectId(crits_id)}, {'$set': {'status': status}}) # this actually isn't an error, it does not update if the value is the same as previous #if result['nModified'] != 1: #logging.error("unable to update crits indicator {}: update count = {}".format(crits_id, reuslt['nModified'])) return result['nModified']
Example #3
Source File: databasemanager.py From TradzQAI with Apache License 2.0 | 7 votes |
def __init__(self, name=None, host='127.0.0.1', port=27017, clients=None): ''' args: Clients : dict filled with name of client and MongoClient dict(name = client) ''' self.clients = dict() # Init default values self.last_client = None self.last_db = None self.last_collection = None self.runner = [] if clients: for name, client in clients.items(): self.addClient(client, name) elif name: self.addClient(MongoClient(host, port), name)
Example #4
Source File: zabbix-mongodb.py From zabbix-mongodb with MIT License | 6 votes |
def connect(self): """Connect to MongoDB""" if self.__conn is None: if self.mongo_user is None: try: self.__conn = MongoClient('mongodb://%s:%s' % (self.mongo_host, self.mongo_port)) except errors.PyMongoError as py_mongo_error: print('Error in MongoDB connection: %s' % str(py_mongo_error)) else: try: self.__conn = MongoClient('mongodb://%s:%s@%s:%s' % (self.mongo_user, self.mongo_password, self.mongo_host, self.mongo_port)) except errors.PyMongoError as py_mongo_error: print('Error in MongoDB connection: %s' % str(py_mongo_error))
Example #5
Source File: mongo_hook.py From mongo_plugin with Apache License 2.0 | 6 votes |
def get_conn(self): """ Fetches PyMongo Client """ conn = self.connection uri = 'mongodb://{creds}{host}{port}/{database}'.format( creds='{}:{}@'.format( conn.login, conn.password ) if conn.login is not None else '', host=conn.host, port='' if conn.port is None else ':{}'.format(conn.port), database='' if conn.schema is None else conn.schema ) # Mongo Connection Options dict that is unpacked when passed to MongoClient options = self.extras # If we are using SSL disable requiring certs from specific hostname if options.get('ssl', False): options.update({'ssl_cert_reqs': CERT_NONE}) return MongoClient(uri, **options)
Example #6
Source File: docker_fixtures.py From aiohttp_admin with Apache License 2.0 | 6 votes |
def mongo_server(unused_port, container_starter): tag = '3.3' image = 'mongo:{}'.format(tag) internal_port = 27017 host_port = unused_port() volume = str(TEMP_FOLDER / 'docker' / 'mongo'), '/data/db' command = '--smallfiles' container = container_starter(image, internal_port, host_port, volume=volume, command=command) params = dict(host='127.0.0.1', port=host_port) def connect(): client = pymongo.MongoClient(**params) test_coll = client.test.test test_coll.find_one() client.close() wait_for_container(connect, image, pymongo.errors.PyMongoError) container['params'] = params return container
Example #7
Source File: tap_mongodb.py From pipelinewise with Apache License 2.0 | 6 votes |
def open_connection(self): """ Open connection """ # Default SSL verify mode to true, give option to disable verify_mode = self.connection_config.get('verify_mode', 'true') == 'true' use_ssl = self.connection_config.get('ssl') == 'true' connection_params = dict(host=self.connection_config['host'], port=int(self.connection_config['port']), username=self.connection_config['user'], password=self.connection_config['password'], authSource=self.connection_config['auth_database'], ssl=use_ssl, replicaSet=self.connection_config.get('replica_set', None), readPreference='secondaryPreferred') # NB: "ssl_cert_reqs" must ONLY be supplied if `SSL` is true. if not verify_mode and use_ssl: connection_params['ssl_cert_reqs'] = ssl.CERT_NONE self.database = MongoClient(**connection_params)[self.connection_config['database']]
Example #8
Source File: arctic_enable_sharding.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def main(): usage = """usage: %prog [options] arg1=value, arg2=value Enables sharding on the specified arctic library. """ setup_logging() parser = optparse.OptionParser(usage=usage) parser.add_option("--host", default='localhost', help="Hostname, or clustername. Default: localhost") parser.add_option("--library", help="The name of the library. e.g. 'arctic_jblackburn.lib'") (opts, _) = parser.parse_args() if not opts.library or '.' not in opts.library: parser.error('must specify the full path of the library e.g. arctic_jblackburn.lib!') print("Enabling-sharding: %s on mongo %s" % (opts.library, opts.host)) c = pymongo.MongoClient(get_mongodb_uri(opts.host)) credentials = get_auth(opts.host, 'admin', 'admin') if credentials: authenticate(c.admin, credentials.user, credentials.password) store = Arctic(c) enable_sharding(store, opts.library)
Example #9
Source File: config.py From picoCTF with MIT License | 6 votes |
def get_db(): """Retrieve a MongoDB client using the settings in db_conf.""" global db if not db: if db_conf["MONGO_USER"] or db_conf["MONGO_PASS"]: mongo_client = MongoClient( "mongodb://{}:{}@{}:{}/{}?authMechanism=SCRAM-SHA-1".format( db_conf["MONGO_USER"], db_conf["MONGO_PASS"], db_conf["MONGO_HOST"], db_conf["MONGO_PORT"], db_conf["MONGO_DB_NAME"], ) ) else: mongo_client = MongoClient( "mongodb://{}:{}/{}".format( db_conf["MONGO_HOST"], db_conf["MONGO_PORT"], db_conf["MONGO_DB_NAME"], ) ) db = mongo_client[db_conf["MONGO_DB_NAME"]] return db
Example #10
Source File: mongo_communicator.py From deep_architect with MIT License | 6 votes |
def refresh_data(data, collection_name, refresh_period, host, port): client = MongoClient(host, port) db = client['deep-architect'] collection = db[collection_name] while collection.find_one_and_update( { '$and': [{ '_id': data['_id'] }, { STARTTIME_KEY: { '$ne': None } }, { ENDTIME_KEY: { '$eq': None } }] }, {'$currentDate': { REFRESH_KEY: True }}, return_document=ReturnDocument.AFTER): logger.debug('Refreshed data for id %s', str(data['_id'])) time.sleep(refresh_period)
Example #11
Source File: test_db_repository.py From pepy with MIT License | 6 votes |
def test_retrieve_project(mongo_client: MongoClient, repository: ProjectRepository): data = { "name": "climoji", "total_downloads": 1100, "downloads": { "2020-04-01": [["2.0", 30]], "2020-04-02": [["2.0", 10]], "2020-03-31": [["2.0", 40]], "2020-04-03": [["2.0", 30]], }, } query = {"name": "climoji"} mongo_client.pepy_test.projects.replace_one(query, data, upsert=True) result = repository.get("climoji") assert ProjectName("climoji") == result.name assert datetime.date(2020, 3, 31) == result.min_date assert Downloads(1100) == result.total_downloads expected_last_downloads = [ ProjectVersionDownloads(datetime.date(2020, 3, 31), "2.0", Downloads(40)), ProjectVersionDownloads(datetime.date(2020, 4, 1), "2.0", Downloads(30)), ProjectVersionDownloads(datetime.date(2020, 4, 2), "2.0", Downloads(10)), ProjectVersionDownloads(datetime.date(2020, 4, 3), "2.0", Downloads(30)), ] assert expected_last_downloads == result.last_downloads()
Example #12
Source File: Config.py From watchdog with Apache License 2.0 | 6 votes |
def getMongoConnection(cls): mongoHost = cls.readSetting("Mongo", "Host", cls.default['mongoHost']) mongoPort = cls.readSetting("Mongo", "Port", cls.default['mongoPort']) mongoDB = cls.getMongoDB() mongoUsername = cls.readSetting("Mongo", "Username", cls.default['mongoUsername']) mongoPassword = cls.readSetting("Mongo", "Password", cls.default['mongoPassword']) mongoUsername = urllib.parse.quote( mongoUsername ) mongoPassword = urllib.parse.quote( mongoPassword ) try: if mongoUsername and mongoPassword: mongoURI = "mongodb://{username}:{password}@{host}:{port}/{db}".format( username = mongoUsername, password = mongoPassword, host = mongoHost, port = mongoPort, db = mongoDB ) connect = pymongo.MongoClient(mongoURI, connect=False) else: connect = pymongo.MongoClient(mongoHost, mongoPort, connect=False) except: sys.exit("Unable to connect to Mongo. Is it running on %s:%s?"%(mongoHost,mongoPort)) return connect[mongoDB]
Example #13
Source File: databasemanager.py From TradzQAI with Apache License 2.0 | 6 votes |
def addClient(self, client, name): ''' Add a new client args: client : MongoClient object name : Name of client as str ''' dbmErrorHandler.ClientNameError(name) dbmErrorHandler.ClientError(client) self.last_client = name new_client = dict( client = client, databases = dict() ) self.clients[name] = new_client
Example #14
Source File: basic02.py From Python24 with MIT License | 5 votes |
def test01(): """ mongodb云服务链接 注意点:登陆的用户和密码并不是Mongodb注册的账号密码,而是为这个数据库创建的用户的用户名和密码 """ client = pymongo.MongoClient("mongodb+srv://Hao:123456Hao@python24-urhj5.mongodb.net/test?retryWrites=true") # 使用python24这个数据 db = client.get_database('Python24') # 获得试用的table表 collection = db.get_collection('Test') print(collection.name)
Example #15
Source File: database.py From cascade-server with Apache License 2.0 | 5 votes |
def pymongo(): mongo_host = settings.load()['database']['mongo'].get('host', '127.0.0.1') mongo_port = settings.load()['database']['mongo'].get('port', '27017') return MongoClient(host=mongo_host, port=mongo_port)[name]
Example #16
Source File: pipelines.py From video_url_crawler_demo with GNU General Public License v3.0 | 5 votes |
def __init__(self, config): db_url = "mongodb://" + \ ('%s:%s@'%(config['user'], config['passwd']) if config['auth'] else '') + \ ('%s:%s/%s' % (config['server'], config['port'], config['db'])) print(db_url) self.client = pymongo.MongoClient(db_url) self.db = self.client[config['db']] self.collection = self.db[config['collection']]
Example #17
Source File: test_fastsync_tap_mongodb.py From pipelinewise with Apache License 2.0 | 5 votes |
def test_close_connection(self): """ Test close_connection method, It should call the close method on the client """ self.mongo.database = Mock(spec_set=Database) client = Mock(spec_set=MongoClient) client.return_value.close.side_effect = True type(self.mongo.database).client = PropertyMock(return_value=client) self.mongo.close_connection() client.close.assert_called_once()
Example #18
Source File: db.py From fairtest with Apache License 2.0 | 5 votes |
def connect_to_client(hostname, port): """ Connects to a MongoDB server """ try: client = MongoClient(hostname, port) return client except Exception, error: print error # TODO: Add custom exception here raise
Example #19
Source File: database.py From super-simple-distributed-keras with MIT License | 5 votes |
def __init__(self, collection, mongo_host='localhost'): """Initialize. Args: collection (string): The name of the Mongo collection to use. mongo_host (string): The host where the controller DB lives. """ client = MongoClient(mongo_host) self.collection = client[collection]
Example #20
Source File: pipelines.py From Python24 with MIT License | 5 votes |
def open_spider(self, item): # 客户端对象 self.client = pymongo.MongoClient("mongodb+srv://Hao:123456Hao@python24-urhj5.mongodb.net/test?retryWrites=true") # 数据库对象 self.db = self.client.get_database('Python24') # 集合对象 self.collection = self.db.get_collection('Douban')
Example #21
Source File: pipelines.py From crawler_examples with Apache License 2.0 | 5 votes |
def __init__(self): client = MongoClient() db = client['doubanMovies'] self.col = db['MOVIE']
Example #22
Source File: data_spider.py From dialogbot with Apache License 2.0 | 5 votes |
def __init__(self): self.conn = pymongo.MongoClient(mongo_host, mongo_port) self.db = self.conn['medical_dict'] self.col = self.db['data']
Example #23
Source File: data_2_db.py From dialogbot with Apache License 2.0 | 5 votes |
def __init__(self): self.conn = pymongo.MongoClient() cur_dir = os.path.abspath(os.path.dirname(__file__)) self.db = self.conn['medical_dict'] self.col = self.db['data'] first_words = [i.strip() for i in open(os.path.join(cur_dir, 'first_name.txt'))] alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] self.stop_words = first_words + alphabets + nums self.key_dict = { '医保疾病': 'yibao_status', "患病比例": "get_prob", "易感人群": "easy_get", "传染方式": "get_way", "就诊科室": "cure_department", "治疗方式": "cure_way", "治疗周期": "cure_lasttime", "治愈率": "cured_prob", '药品明细': 'drug_detail', '药品推荐': 'recommand_drug', '推荐': 'recommand_eat', '忌食': 'not_eat', '宜食': 'do_eat', '症状': 'symptom', '检查': 'check', '成因': 'cause', '预防措施': 'prevent', '所属类别': 'category', '简介': 'desc', '名称': 'name', '常用药品': 'common_drug', '治疗费用': 'cost_money', '并发症': 'acompany' } self.cuter = jieba
Example #24
Source File: common.py From picoCTF with MIT License | 5 votes |
def get_conn(): """Get a connection to the testing database.""" global db if db is None: client = pymongo.MongoClient(host="127.0.0.1", port=27018) db = client[TESTING_DB_NAME] return db
Example #25
Source File: mongodb_dump.py From apk_api_key_extractor with Apache License 2.0 | 5 votes |
def __init__(self): self.client = pymongo.MongoClient(config.mongodb["address"], int(config.mongodb["port"]), username=config.mongodb["user"], password=config.mongodb["password"]) self.db = self.client[config.mongodb["name"]] self.collection = self.db[COLLECTION_NAME] self.strings_collection = self.db[STRINGS_COLLECTION_NAME]
Example #26
Source File: model_generator.py From genderizer with MIT License | 5 votes |
def generate(mongourl, database, collection, lang): c = MongoClient(mongourl) tweets = c[database][collection].find() trainer = Trainer(tokenizer) for tweet in tweets: trainer.train(tweet['tweet'], tweet['gender']) modelFileName = 'model_{}.txt'.format(lang) with open(modelFileName, 'wb') as modelFile: cPickle.dump(trainer.data, modelFile, cPickle.HIGHEST_PROTOCOL) print('OK : generated trained data has been writen in the file "{}"'. format(modelFileName))
Example #27
Source File: mongoNamesCollection.py From genderizer with MIT License | 5 votes |
def init(cls): cls.mongoclient = MongoClient(cls.mongodbURL) cls.collection = cls.mongoclient['test'][cls.collectionName] if not cls.collection.count() > 0: cls.loadFromSource()
Example #28
Source File: db.py From im with GNU General Public License v3.0 | 5 votes |
def _connect_mongo(self, url, db): if MONGO_AVAILABLE: client = MongoClient(url) self.connection = client[db] self.db_type = DataBase.MONGO return True else: return False
Example #29
Source File: tools.py From Auto_Record_Matsuri with MIT License | 5 votes |
def __init__(self, db: str): client = pymongo.MongoClient("mongodb://127.0.0.1:27017/") _db = client["Video"] self.db = _db[db] self.logger = logging.getLogger('run.db')
Example #30
Source File: mongo.py From news-popularity-prediction with Apache License 2.0 | 5 votes |
def get_collection_documents_generator(client, database_name, collection_name, spec, latest_n, sort_key, batch_size=None): """ This is a python generator that yields tweets stored in a mongodb collection. Tweet "created_at" field is assumed to have been stored in the format supported by MongoDB. Inputs: - client: A pymongo MongoClient object. - database_name: The name of a Mongo database as a string. - collection_name: The name of the tweet collection as a string. - spec: A python dictionary that defines higher query arguments. - latest_n: The number of latest results we require from the mongo document collection. - sort_key: A field name according to which we will sort in ascending order. Yields: - document: A document in python dictionary (json) format. """ mongo_database = client[database_name] collection = mongo_database[collection_name] collection.create_index(sort_key) if latest_n is not None: skip_n = collection.count() - latest_n if collection.count() - latest_n < 0: skip_n = 0 if batch_size is None: cursor = collection.find(filter=spec).sort([(sort_key, ASCENDING), ]) else: cursor = collection.find(filter=spec).sort([(sort_key, ASCENDING), ]).batch_size(batch_size) cursor = cursor[skip_n:] else: if batch_size is None: cursor = collection.find(filter=spec).sort([(sort_key, ASCENDING), ]) else: cursor = collection.find(filter=spec).sort([(sort_key, ASCENDING), ]).batch_size(batch_size) for document in cursor: yield document