2013-05-14 62 views
3

我想在我的mongoDB中設置fb_id和ts的複合索引。所以,我所做的:設置複合索引時出錯

PRIMARY> db.sessions.ensureIndex({ fb_id: 1, ts: 1 }, { unique:true }); 

但我得到以下錯誤:

E11000 duplicate key error index: tracking.sessions.$fb_id_1_ts_1 dup key: { : null, : null } 

所以我使用db.sessions.getIndexes()選中此集合中的索引,我得到:

[ 
    { 
     "v" : 1, 
     "key" : { 
      "_id" : 1 
     }, 
     "ns" : "tracking.sessions", 
     "name" : "_id_" 
    } 
] 

它不看起來像一個重複的鑰匙給我。我在這裏做錯了什麼?

回答

2

MongoDB的告訴你,你有幾個文件(不止一個)具有相同fb_idts值,null值。換句話說,有文件沒有fb_idts字段。所以它違反了整個系列的獨特約束。

作爲一種解決方法,您應該查看sparse索引。從文檔報價:

Sparse indexes only contain entries for documents that have the indexed field. Any document that is missing the field is not indexed. The index is 「sparse」 because of the missing documents when values are missing.

參見:

希望有所幫助。