2017-09-28 3 views
0

作成することはできません、ここに私のデータです:Mongoのまばらなインデックスは、私がまばらな一意のインデックスを作成しようとしている

db.trees.createIndex({ id_name: 1, item_type: -1 }, { sparse: true, unique: true }) 

エラー:

{ 
    "ok" : 0, 
    "errmsg" : "E11000 duplicate key error index: ccp.trees.$id_name_1_item_type_-1 dup key: { : null, : 3 }", 
    "code" : 11000, 
    "codeName" : "DuplicateKey" 
} 

rs-ds119302:PRIMARY> db.trees.find({id_name:{$exists:true}, item_type:{$exists:true}}, {id_name: 1, item_type: 1}) 
{ "_id" : ObjectId("59417bf6748a2469ab093183"), "item_type" : 1, "id_name" : "AA" } 
{ "_id" : ObjectId("59417bd7bc99346962fd289e"), "item_type" : 1, "id_name" : "BB" } 
{ "_id" : ObjectId("59417bb4587803690be313bb"), "item_type" : 1, "id_name" : "CC" } 
{ "_id" : ObjectId("599b6ff11e4d88264658e497"), "id_name" : "DD", "item_type" : 1 } 
{ "_id" : ObjectId("599f3fa8442b9d46e2340389"), "id_name" : "EE", "item_type" : 1 } 

インデックスコマンドを作成しますそれについての考えは?だから、索引付けされたドキュメントを見つけるためにあなたのフィルタが設定された2つのプロパティの少なくとも1を持っている、散在指標になるインデックスすべての文書ので、正しくない

Sparse compound indexes that only contain ascending/descending index keys will index a document as long as the document contains at least one of the keys.

: は

答えて

1

documentationをチェックアウトしていただきありがとうございます。代わりに、重複したキーを表示する必要がある次のクエリを実行する必要があります。

db.trees.find({$or: [ { "id_name": { $exists: true } }, { "item_type": { $exists: true } } ] }) 
関連する問題