3

私はこのようなユニークなインデックスを作成します:

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 } 

なぜ?

4

1 に答える 1

17

notification: NULLコレクション内に、通知フィールドが設定されているドキュメントまたは設定されていないドキュメントが既にある可能性があります。フィールドが設定されていない場合は、null と見なされます。一意のインデックスではフィールドごとに 1 つの値しか許可されないため、フィールドが設定されていないドキュメントを 2 つ持つことはできません。sparseインデックスの作成中にオプションを使用することで、これを回避できます。このようなものが機能するはずです(既存のインデックスをドロップした後notification

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

参照: mongo のスパース インデックスと null 値

于 2012-06-02T16:30:57.573 に答える