Dealing with mongodb unique, sparse, compound indexes

前端 未结 3 1324
爱一瞬间的悲伤
爱一瞬间的悲伤 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:32

    A hash index ended up being sufficient for this

    0 讨论(0)
  • 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 } } }
    )
    
    0 讨论(0)
  • 2021-01-11 14:38

    A sparse index avoids indexing a field that doesn't exist. A unique index avoid documents being inserted that have the same field values. Unfortunately as of MongoDB 2.6.7, the unique constraint is always enforced even when creating a compound index (indexing two or more fields) with the sparse and unique properties.

    Example:

    db = db.connect("test");
    db.a.drop();
    db.a.insert([
        {},
        {a : 1},
        {b : 1},
        {a : 1, b : 1}
    ]);
    db.a.ensureIndex({a:1,b:1},  { sparse: true, unique: true } );
    db.a.insert({a : 1}); // throws Error but wanted insert to be valid.
    

    However, it works as expected for a single index field with sparse and unique properties. I feel like this is a bug that will get fixed in future releases.

    Anyhow, here are two solutions to get around this problem.

    1) Add a non-null hash field to each document that is only computed when all the required fields for checking the uniqueness are supplied. Then create a sparse unique index on the hash field.

    function createHashForUniqueCheck(obj){
        if( obj.firstName && obj.id){
            return MD5( String( obj.firstName) + String(obj.id) );
        }
        return null;
    }
    

    2) On the application side, check for uniqueness before insertion into Mongodb. :-)

    • sparse index doc
    0 讨论(0)
提交回复
热议问题