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:

hope helps.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -