为什么 DuplicateKeyError: E11000 重复键错误索引: test.test.$notification_1 dup key: { : null }

2023-12-19

我创建这样的唯一索引:

self.db_database[co_name].ensure_index([('src_md5',-1),('src_time',-1),('src_size',-1)],unique=True)
self.db_database[co_name].ensure_index(('notification'),unique=True)
self.db_database[co_name].ensure_index(('version'),unique=True)`  

在插入之前我创建一条记录,如下所示:

self.db_database[co_name].insert({"notification":"yes","file_md5":-1,"file_size":-1,"file_time":-1,"bypass":0,"server_list":[],"ok_to_download":0,"force_to_download":-1,"idx":0},safe=True)`  

然后我插入一些这样的信息:

collection.insert({"src_host":src_host,"src_path":src_path,"src_name":src_name,"src_md5":src_md5,"src_time":src_time,"src_size":src_size,"version":idx},safe=True)`  

它会引发错误:

DuplicateKeyError: E11000 duplicate key error index: data_transfer.nova_mon_test.log.small_20120517202918765432.$notification_1  dup key: { : null } 

WHY?


您的收藏中可能已经有一份文档,其中有notification: NULL或未设置通知字段的文档。如果某个字段未设置,则该字段被视为空。由于唯一索引只允许每个字段有一个值,因此不能有两个没有设置字段的文档。您还可以使用sparse创建索引时的选项。像这样的东西应该可以工作(在删除已经存在的索引之后)notification:

self.db_database[co_name].ensure_index(('notification'),unique=True,sparse=True)

也可以看看:mongo 中的稀疏索引和空值 https://stackoverflow.com/questions/8608567/sparse-indexes-and-null-values-in-mongo

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 DuplicateKeyError: E11000 重复键错误索引: test.test.$notification_1 dup key: { : null } 的相关文章

随机推荐