Error while setting up compound index

前端 未结 1 711
你的背包
你的背包 2021-01-20 03:19

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 } );
         


        
相关标签:
1条回答
  • 2021-01-20 03:35

    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:

    • sparse indexes and null values in mongo
    • why DuplicateKeyError: E11000 duplicate key error index: test.test.$notification_1 dup key: { : null }

    Hope that helps.

    0 讨论(0)
提交回复
热议问题