Python pymongo.IndexModel() Examples

The following are 4 code examples of pymongo.IndexModel(). 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: indexes.py    From umongo with MIT License 6 votes vote down vote up
def parse_index(index, base_compound_field=None):
    keys = None
    args = {}
    if isinstance(index, IndexModel):
        keys = index.document['key'].items()
        args = {k: v for k, v in index.document.items() if k != 'key'}
    elif isinstance(index, (tuple, list)):
        # Compound indexes
        keys = [explicit_key(e) for e in index]
    elif isinstance(index, str):
        keys = [explicit_key(index)]
    elif isinstance(index, dict):
        assert 'key' in index, 'Index passed as dict must have a `key` entry'
        assert hasattr(index['key'], '__iter__'), '`key` entry must be iterable'
        keys = [explicit_key(e) for e in index['key']]
        args = {k: v for k, v in index.items() if k != 'key'}
    else:
        raise TypeError('Index type must be <str>, <list>, <dict> or <pymongo.IndexModel>')
    if base_compound_field:
        keys.append(explicit_key(base_compound_field))
    return IndexModel(keys, **args) 
Example #2
Source File: base_consumer.py    From distributed_framework with Apache License 2.0 5 votes vote down vote up
def __init__(self, function_result_status_persistance_conf, queue_name):
        self.function_result_status_persistance_conf = function_result_status_persistance_conf
        if self.function_result_status_persistance_conf.is_save_status:
            task_status_col = self.mongo_db_task_status.get_collection(queue_name)
            # params_str 如果很长,必须使用TEXt或HASHED索引。
            task_status_col.create_indexes([IndexModel([("insert_time_str", -1)]), IndexModel([("insert_time", -1)]),
                                            IndexModel([("params_str", pymongo.TEXT)]), IndexModel([("success", 1)])
                                            ], )
            task_status_col.create_index([("utime", 1)],
                                         expireAfterSeconds=function_result_status_persistance_conf.expire_seconds)  # 只保留7天。
            self._mongo_bulk_write_helper = MongoBulkWriteHelper(task_status_col, 100, 2)
            self.task_status_col = task_status_col 
Example #3
Source File: test_mongo_store.py    From coal-mine with Apache License 2.0 5 votes vote down vote up
def test_init_index_compatibility(self):
        collection = self.db['canaries']
        collection.create_indexes([IndexModel([('id', ASCENDING)])])
        existing_indexes = collection.index_information()
        self.assertNotIn('unique', existing_indexes['id_1'])
        MongoStore(self.db_hosts, self.db_name, None, None)
        new_existing_indexes = collection.index_information()
        self.assertTrue(new_existing_indexes['id_1']['unique'])
        # For code coverage, testing when is_unique is already True as it
        # should be.
        MongoStore(self.db_hosts, self.db_name, None, None) 
Example #4
Source File: pipelines.py    From pornhubbot with MIT License 5 votes vote down vote up
def __init__(self):
        clinet = pymongo.MongoClient("localhost", 27017)
        db = clinet["PornHub"]
        self.PhRes = db["PhRes"]
        idx = IndexModel([('link_url', ASCENDING)], unique=True)
        self.PhRes.create_indexes([idx])
        # if your existing DB has duplicate records, refer to:
        # https://stackoverflow.com/questions/35707496/remove-duplicate-in-mongodb/35711737