gpt4 book ai didi

python - 为什么 DuplicateKeyError : E11000 duplicate key error index: test. test.$notification_1 dup key: { : null }

转载 作者:可可西里 更新时间:2023-11-01 09:18:02 26 4
gpt4 key购买 nike

我像这样创建唯一索引:

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 } 

为什么?

最佳答案

您的集合中可能已经有一个文档,其中有 notification: NULL 或没有设置通知字段的文档。如果一个字段没有设置,那么它被认为是空的。因为唯一索引只允许每个字段有一个值,所以不能有两个没有字段集的文档。您也可以在创建索引时使用 sparse 选项来解决这个问题。像这样的东西应该可以工作(在 notification 上删除已经存在的索引之后:

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

另请参阅:sparse indexes and null values in mongo

关于python - 为什么 DuplicateKeyError : E11000 duplicate key error index: test. test.$notification_1 dup key: { : null },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com