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 Project: TradzQAI   Author: kkuette   File: databasemanager.py    License: Apache License 2.0 7 votes vote down vote up
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 #2
Source Project: ACE   Author: IntegralDefense   File: crits.py    License: Apache License 2.0 7 votes vote down vote up
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 Project: pipelinewise   Author: transferwise   File: db.py    License: Apache License 2.0 7 votes vote down vote up
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 #4
Source Project: TradzQAI   Author: kkuette   File: databasemanager.py    License: Apache License 2.0 6 votes vote down vote up
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 #5
Source Project: watchdog   Author: flipkart-incubator   File: Config.py    License: Apache License 2.0 6 votes vote down vote up
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 #6
Source Project: arctic   Author: man-group   File: arctic_enable_sharding.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #7
Source Project: deep_architect   Author: negrinho   File: mongo_communicator.py    License: MIT License 6 votes vote down vote up
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 #8
Source Project: picoCTF   Author: picoCTF   File: config.py    License: MIT License 6 votes vote down vote up
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 #9
Source Project: zabbix-mongodb   Author: omni-lchen   File: zabbix-mongodb.py    License: MIT License 6 votes vote down vote up
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 #10
Source Project: pepy   Author: psincraian   File: test_db_repository.py    License: MIT License 6 votes vote down vote up
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 #11
Source Project: pipelinewise   Author: transferwise   File: tap_mongodb.py    License: Apache License 2.0 6 votes vote down vote up
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 #12
Source Project: aiohttp_admin   Author: aio-libs   File: docker_fixtures.py    License: Apache License 2.0 6 votes vote down vote up
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 #13
Source Project: mongo_plugin   Author: airflow-plugins   File: mongo_hook.py    License: Apache License 2.0 6 votes vote down vote up
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 #14
Source Project: everyclass-server   Author: everyclass   File: mongodb.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def init_pool(current_application) -> None:
    """创建连接池,保存在 app 的 mongo 属性中"""
    current_application.mongo = MongoClient(**current_application.config['MONGODB']) 
Example #15
Source Project: everyclass-server   Author: everyclass   File: mongodb.py    License: Mozilla Public License 2.0 5 votes vote down vote up
def get_connection() -> database.Database:
    """在连接池中获得连接"""
    if not has_app_context():
        config = get_config()
        return MongoClient(**config.MONGODB).get_database(config.MONGODB_DB)
    return current_app.mongo.get_database(current_app.config['MONGODB_DB']) 
Example #16
Source Project: iSDX   Author: sdn-ixp   File: ribm.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self,table_suffix,name):
        self.name = name + "_" + str(table_suffix)
        self.client = MongoClient(globs.MONGODB_HOST, globs.MONGODB_PORT)
        self.db = self.client['demo']
        self.session = self.db[self.name] 
Example #17
Source Project: jiji-with-tensorflow-example   Author: unageanu   File: trade_results_loader.py    License: MIT License 5 votes vote down vote up
def retrieve_trade_data(self):
        client = pymongo.MongoClient(
            TradeResultsLoader.DB_HOST, TradeResultsLoader.DB_PORT)
        collection = client[TradeResultsLoader.DB][TradeResultsLoader.COLLECTION]
        cursor = collection.find().sort("entered_at")
        return pd.DataFrame(list(cursor)) 
Example #18
Source Project: friendly-telegram   Author: friendly-telegram   File: raphielgang.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def MONGOCLIENT(self):
        if self.MONGO_URI is not None and self.mongoclient is None:
            self.mongoclient = pymongo.MongoClient(self.MONGO_URI, 27017, serverSelectionTimeoutMS=1)
        return self.mongoclient 
Example #19
Source Project: invana-bot   Author: invanalabs   File: mongodb.py    License: MIT License 5 votes vote down vote up
def connect(self):
        self._client = MongoClient(self._connection_string) 
Example #20
Source Project: invana-bot   Author: invanalabs   File: mongodb.py    License: MIT License 5 votes vote down vote up
def write(self):

        if isinstance(self._client, MongoClient):
            for doc in self._docs:
                entry = self.get_object(doc=doc)
                if entry is None:
                    self._client[self._db][self._collection].insert(doc)
                else:
                    doc['updated'] = datetime.now()
                    self._client[self._db][self._collection].update_one({self._unique_key: doc.get(self._unique_key)},
                                                                        {"$set": doc}) 
Example #21
Source Project: invana-bot   Author: invanalabs   File: mongodb.py    License: MIT License 5 votes vote down vote up
def disconnect(self):
        if isinstance(self._client, MongoClient):
            self._client.close() 
Example #22
Source Project: invana-bot   Author: invanalabs   File: mongodb.py    License: MIT License 5 votes vote down vote up
def connect(self):
        self._client = MongoClient(self._connection_string) 
Example #23
Source Project: invana-bot   Author: invanalabs   File: mongodb.py    License: MIT License 5 votes vote down vote up
def read(self):
        if isinstance(self._client, MongoClient):
            for doc in self._client[self._db][self._collection].find(self._query, projection=self._fields):
                yield doc 
Example #24
Source Project: invana-bot   Author: invanalabs   File: mongodb.py    License: MIT License 5 votes vote down vote up
def __init__(self, settings):
        self.CONNECTION_URI = settings.get('INVANA_BOT_SETTINGS', {}).get('HTTPCACHE_STORAGE_SETTINGS', {}).get(
            "CONNECTION_URI", None)
        self.database_name = settings.get('INVANA_BOT_SETTINGS', {}).get('HTTPCACHE_STORAGE_SETTINGS', {}).get(
            "DATABASE_NAME", None)
        self.cache_expiry_time = settings.get('INVANA_BOT_SETTINGS', {}).get('HTTPCACHE_STORAGE_SETTINGS', {}).get(
            "EXPIRY_TIME", None)
        self.collection_name = settings.get('INVANA_BOT_SETTINGS', {}).get('HTTPCACHE_STORAGE_SETTINGS', {}).get(
            "COLLECTION_NAME", None)
        self.db_client = pymongo.MongoClient(self.CONNECTION_URI)
        self.db = self.db_client[self.database_name] 
Example #25
Source Project: invana-bot   Author: invanalabs   File: base.py    License: MIT License 5 votes vote down vote up
def create_mongodb_connection(data_storage=None):
        db_client = MongoClient(data_storage.get("connection_uri"))
        db_conn = db_client[data_storage.get("database_name")]
        return db_conn 
Example #26
Source Project: invana-bot   Author: invanalabs   File: mongodb.py    License: MIT License 5 votes vote down vote up
def __init__(self,
                 connection_uri=None,
                 database_name=None,
                 collection_name=None):
        # TODO - implement caching the db_clients specific to collection and database
        if None in [connection_uri, database_name, collection_name]:
            raise Exception("connection_uri, database_name, collection_name should be provided.")

        self.db_client = pymongo.MongoClient(connection_uri)
        self.db = self.db_client[database_name]
        self.collection = self.db[collection_name] 
Example #27
Source Project: invana-bot   Author: invanalabs   File: mongodb.py    License: MIT License 5 votes vote down vote up
def process_item(self, item, spider):
        if self.collection is None:
            raise Exception("pymongo.MongoClient() it not called in the Pipeline, please make the connection first")
        data = dict(item)
        data['updated'] = datetime.now()
        self.collection.insert(data)
        # print("Item added to Storage", data)
        return item 
Example #28
Source Project: TradzQAI   Author: kkuette   File: databasemanager.py    License: Apache License 2.0 5 votes vote down vote up
def ClientError(value):
        if not isinstance(value, MongoClient):
            raise TypeError("%s in not a valid type for client it should be MongoClient" % type(value)) 
Example #29
Source Project: iHealth_site   Author: iHealth-ecnu   File: models.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        '''初始化'''
        # 连接 mongo 数据库,获得数据指定集合
        self.client = pymongo.MongoClient('mongodb://%s:%s@%s:%d/%s'%(settings.
MONGO_USER,settings.MONGO_PWD,settings.MONGO_HOST,settings.MONGO_PORT,settings.
MONGO_AUTHDB))[settings.MONGO_DBNAME]
        self.articles = self.client['articles'] 
Example #30
Source Project: iHealth_site   Author: iHealth-ecnu   File: models.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        '''初始化'''
        # 连接 mongo 数据库,获得数据指定集合
        self.client = pymongo.MongoClient('mongodb://%s:%s@%s:%d/%s'%(settings.
MONGO_USER,settings.MONGO_PWD,settings.MONGO_HOST,settings.MONGO_PORT,settings.
MONGO_AUTHDB))[settings.MONGO_DBNAME]
        self.users = self.client['users']