mongodb 对象唯一索引_MongoDB可选唯一索引

I have a MongoDB schema for users that looks something like this:

{

userId: "some-string",

anonymousId: "some-other-string",

project: {"$oid": "56d06bb6d9f75035956fa7ba"}

}

Users must have either a userId or an anonymousId. As users belong to a project, the model also has a reference called project, which links to the project collection.

Any userId or anonymousId value has to be unique per project, so I created two compound indexes as follows:

db.users.createIndex({ "userId": 1, "project": 1 }, { unique: true })

db.users.createIndex({ "anonymousId": 1, "project": 1 }, { unique: true })

However as not both userId and anonymousId have to be provided but just either one of them, MongoDB throws a duplicate key error for null values (for example if there is a second user with a provided anonymousId but no userId).

I therefore tried to add a sparse: true flag to the compound indexes, but this obviously only works if both fields are empty. I also tried adding the sparse flag only to the fields and not the compound indexes, but this doesn't work either.

To give an example, let's say I have the following three users in the collection:

{ userId: "user1", anonymousId: null, project: {"$oid": "56d06bb6d9f75035956fa7ba"}}

{ userId: "user2", anonymousId: "anonym", project: {"$oid": "56d06bb6d9f75035956fa7ba"}}

{ userId: "user3", anonymousId: "random", project: {"$oid": "56d06bb6d9f75035956fa7ba"}}

The following should be possible:

I want to be able to insert another user {userId: "user4", anonymousId: null} for the same project (without getting a duplicate key error)

However if I try to insert another user with {userId: "user3"} or another user with {anonymousId: "random"} there should be a duplicate key error

How else can I achieve this?

解决方案

If you are using MongoDB 3.2, you can use unique partial index instead of sparse index.

Partial index is actually recommended over sparse index

Example

db.users.createIndex({ "userId": 1, "project": 1 },

{ unique: true, partialFilterExpression:{

userId: { $exists: true, $gt : { $type : 10 } } } })

db.users.createIndex({ "anonymousId": 1, "project": 1 },

{ unique: true, partialFilterExpression:{

anonymouseId: { $exists: true, $gt : { $type : 10 } } } })

In above example, Unique index will only be created when userId is present and doesn't contain null value. Same holds true to anonymousId too.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值