【发布时间】:2020-10-10 06:38:18
【问题描述】:
尝试使用 PyMongo 在 mongoDb 中创建复合唯一索引以避免重复。 这是我的代码;
index = collection.create_index([('date', 1), ('name', 1)], {'unique' : True})
它引发了以下错误。
AttributeError: 'dict' object has no attribute '_pinned_address'
如果我将参数中的 dict 更改为元组,我会得到相同的错误,但 'tuple' 对象除外。 我只是继续通过 mongo compass 创建索引,但想知道 PyMongo 的解决方案。 有什么想法吗?
【问题讨论】:
-
unique是一个 kwarg,而不是一个 arg。试试看:index = collection.create_index([('date', 1), ('name', 1)], unique=True)
标签: python mongodb indexing pymongo