我正在尝试使用pymongo创建索引,但由于错误而失败
File "D:/Users/Dims/Design/EnergentGroup/Python GIS Developer/worker/Approach03\sentinel\mongo.py", line 46, in get_results_collection
results_collection.create_index(["uwi", "date_part"], name=index_name, unique=True)
File "C:\Anaconda3\lib\site-packages\pymongo\collection.py", line 1386, in create_index
name = kwargs.setdefault("name", helpers._gen_index_name(keys))
File "C:\Anaconda3\lib\site-packages\pymongo\helpers.py", line 49, in _gen_index_name
return _UUNDER.join(["%s_%s" % item for item in keys])
File "C:\Anaconda3\lib\site-packages\pymongo\helpers.py", line 49, in <listcomp>
return _UUNDER.join(["%s_%s" % item for item in keys])
TypeError: not enough arguments for format string
显然,在创建默认索引名称的代码库中发生错误,这是
def _gen_index_name(keys):
"""Generate an index name from the set of fields it is over."""
return _UUNDER.join(["%s_%s" % item for item in keys])
显然不正确。
我的代码如下:
index_name = 'uwi_date_part'
if index_name not in index_information:
print("Creating index '%s'..." % index_name)
results_collection.create_index(["uwi", "date_part"], name=index_name, unique=True)
index_name = 'uwi'
if index_name not in index_information:
print("Creating index '%s'..." % index_name)
results_collection.create_index("uwi", name=index_name, unique=False)
如何克服?
PyMongo不需要以下语法:
results_collection.create_index(["uwi", "date_part"], name=index_name, unique=True)
您要在两个字段“ uwi”和“ date_part”上建立索引吗?仔细选择对字段进行索引的顺序(请参阅优化MongoDB复合索引),以及是按升序还是降序对它们进行索引。
如果要按升序索引“ uwi”和“ date_part”,请执行以下操作:
results_collection.create_index([("uwi", 1), ("date_part", 1)], name=index_name, unique=True)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句