Dealing with mongodb unique, sparse, compound indexes

前端 未结 3 1325
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-11 14:04

Because mongodb will index sparse, compound indexes that contain 1 or more of the indexed fields, it is causing my unique, sparse index to fail because one of those fields

3条回答
  •  广开言路
    2021-01-11 14:34

    https://docs.mongodb.org/manual/core/index-partial/

    As of mongoDB 3.2 you can create partial index to support this as well.

    db.users.createIndex(
       { name: 1, email: 1 },
       { unique: true, partialFilterExpression: { email: { $exists: true } } }
    )
    

提交回复
热议问题