I want to setup compound index on fb_id and ts in my mongoDB. So, I did:
PRIMARY> db.sessions.ensureIndex( { fb_id: 1, ts: 1 }, { unique:true } );
MongoDB tells you that you have several documents (more than one) with the same fb_id
and ts
values, null
values. In other words, there are documents without fb_id
and ts
fields. So, it violates unique constraint across the collection.
As a workaround, you should take a look at sparse indexes. Quote from docs:
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.
See also:
Hope that helps.