mongodb - Error while setting up compound index -
i want setup compound index on fb_id , ts in mongodb. so, did:
primary> db.sessions.ensureindex( { fb_id: 1, ts: 1 }, { unique:true } );
but got following error:
e11000 duplicate key error index: tracking.sessions.$fb_id_1_ts_1 dup key: { : null, : null }
so checked indexes in collection using db.sessions.getindexes()
, got:
[ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "tracking.sessions", "name" : "_id_" } ]
it doesn't duplicate key me. doing wrong here?
mongodb tells you have several documents (more one) same fb_id
, ts
values, null
values. in other words, there documents without fb_id
, ts
fields. so, violates unique constraint across collection.
as workaround, should take @ sparse indexes. quote docs:
sparse indexes contain entries documents have indexed field. document missing field not indexed. index “sparse” because of missing documents when values missing.
see also:
- sparse indexes , null values in mongo
- why duplicatekeyerror: e11000 duplicate key error index: test.test.$notification_1 dup key: { : null }
hope helps.
Comments
Post a Comment