本文整理汇总了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