本文整理匯總了Python中pymongo.IndexModel方法的典型用法代碼示例。如果您正苦於以下問題:Python pymongo.IndexModel方法的具體用法?Python pymongo.IndexModel怎麽用?Python pymongo.IndexModel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pymongo
的用法示例。
在下文中一共展示了pymongo.IndexModel方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: parse_index
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import IndexModel [as 別名]
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)
示例2: __init__
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import IndexModel [as 別名]
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
示例3: test_init_index_compatibility
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import IndexModel [as 別名]
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)
示例4: __init__
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import IndexModel [as 別名]
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