处理mongodb独特,稀疏,复合指数 [英] Dealing with mongodb unique, sparse, compound indexes

查看:176
本文介绍了处理mongodb独特,稀疏,复合指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为 mongodb将索引包含一个或多个索引的稀疏复合索引字段,它导致我的唯一稀疏索引失败,因为其中一个字段是可选的,并且被mongodb强制转换为 null 索引。

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 is optional, and is being coerced to null by mongodb for the purpose of the index.

我需要数据库级保证该字段与其他一些字段的组合的唯一性,并且必须通过一些串联字符串担忧在应用程序级别进行管理我。

I need database-level ensurance of uniqueness for the combination of this field and a few others, and having to manage this at the application level via some concatenated string worries me.

作为替代方案,我考虑将可能为空的索引字段的默认值设置为'null'+ anObjectId ,因为它允许我保留索引而不会导致错误。这看起来像一个敏感(虽然hacky)解决方案?有没有人知道我可以在复合索引上强制执行数据库级唯一性的更好方法?

As an alternative, I considered setting the default value of the possibly null indexed field to 'null ' + anObjectId, because it would allow me to keep the index without causing errors. Does this seem like a sensisble (although hacky) solution? Does anyone know of a better way I could enforce database-level uniqueness on a compound index?

编辑:我被要求详细说明实际问题域,所以它就这样了。

I was asked to elaborate on the actual problem domain a bit more, so here it goes.

我们从客户处获得需要集成到数据库中的大量数据。这些提要包括客户提供的各种(3)唯一标识符,用于更新数据提要刷新时存储在数据库中的版本。我需要将这些标识符的唯一性与客户联系起来,因为相同的标识符可能出现在多个来源中,我们希望允许这样做。

We get large data feeds from our customers that we need to integrate into our database. These feeds include various (3) unique identifiers supplied by the customer that we use for updating the versions we store in our database when the data feeds refresh. I need to tie uniqueness of these identifiers to the customer, because the same identifier could appear from multiple sources, and we want to allow that.

文档结构如下所示:

{
  "identifiers": {
      "identifierA": ...,
      "identifierB": ...,
      "identifierC": ...
  },
  "client": ...
}

因为每个单独的标识符是可选的(至少需要三个中的一个),我需要唯一索引索引的组合客户端(例如,一个索引是客户端加上 identifierA 的组合)。但是,此索引必须仅在标识符存在时发生,但我的mongodb不支持此功能(请参阅上面的超链接)。

Because the each individual identifier is optional (at least one of the three is required), I need to uniquely index the combination of the index with the client (e.g. one index is the combination of client plus identifierA). However, this index must only occur when the identifier exists, but this is not supported my mongodb (see the hyperlink above).

我正在考虑上述解决方案,但我我想听听是否其他人已经解决了这个问题或有建议。

I was considering the above solution, but I would like to hear if anyone else has solved this or has suggestions.

推荐答案

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

从mongoDB 3.2开始,您也可以创建部分索引来支持这一点。

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

这篇关于处理mongodb独特,稀疏,复合指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆