在一个集合的所有文档上具有唯一值的数组 [英] array with unique values over all documents of one collection

查看:58
本文介绍了在一个集合的所有文档上具有唯一值的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况: 我有几个具有相同集合(帐户)的文档,每个文档都有一个名为唯一字符串的数组(字符串)类型的属性.

Situation: I have several documents of the same collection(account), each has an attribute of type array(string) named uniquestrings.

问题:在mongodb中的所有文档中,唯一字符串中的每个条目都必须是唯一的.看来MongoDB/Mongoose没有提供这样的验证(addToSet¹或index:{unique:true}²都不能解决问题).是否有一种模式可以重组我的文档架构,以确保mongodb本身可以对其进行验证?目前,软件本身会先检查它,然后再更新文档.

Problem: Each entry in uniquestrings must be unique over all documents in mongodb. It seems that MongoDB/Mongoose does not offer such a validation (neither addToSet¹ nor index: {unique: true}² solve the problem). Is there a pattern to restructure my document schema to make sure mongodb itself can validate it? At the moment the software itself checks it before updating the document.

例如

account {
  _id: 111,
  uniquestrings: ["a", "b", "c"]
}

account {
  _id: 222,
  uniquestrings: ["d", "e", "f"]
}

例如通过抛出来自mongo的重复错误来防止account(222).uniquestrings.push("a");.

E.g. prevent account(222).uniquestrings.push("a"); by throwing a duplicate error from mongo.

¹数组中的唯一性不足
²数组中的每个项目在整个集合中必须是唯一的

¹ Uniqueness in an array is not enough
² Each item in array has to be unique across the collection

UPDATE1:

更多示例.受影响的架构条目如下所示:

More examples. Affected Schema entry looks like:

var Account = new Schema({
    ...
    uniquestrings: [{type: String, index: {unique: true}}]
    ...
});

现在创建4个帐户凭证时.我只希望1和2都可以,其余的应该失败.

Now when create 4 account documents. I want only 1 and 2 be ok, and rest should fail.

var accountModel1 = new Account.Model({uniquestrings: ["a", "b"]});
accountModel1.save(); // OK
var accountModel2 = new Account.Model({uniquestrings: ["c", "d"]});
accountModel2.save(); // OK
var accountModel3 = new Account.Model({uniquestrings: ["c", "d"]});
accountModel3.save(); // FAIL => Not unique, so far so good
var accountModel4 = new Account.Model({uniquestrings: ["X", "d"]});
accountModel4.save(); // OK => But i Want this to faile because "d" is alreay in use.

推荐答案

如果您愿意将唯一值存储在另一个集合中,则有可能.它的结构如下:

It might be possible, if you are willing to store the unique values in a different collection. It would be structured like this:

{ "uniquestring" : "a", "account" : 111 }
{ "uniquestring" : "b", "account" : 111 }
{ "uniquestring" : "c", "account" : 111 }
{ "uniquestring" : "d", "account" : 222 }
{ "uniquestring" : "e", "account" : 222 }
{ "uniquestring" : "f", "account" : 222 }

我不是Mongoose的专家,但是我相信您可以定义模型来将集合链接在一起,这里的account字段引用帐户集合的_id字段.

I am not an expert with Mongoose, but I believe that you can define Models to link collections together, with the account field here referencing the accounts collection's _id field.

现在,您可以通过简单的索引来强制唯一性:

Now, you can enforce the uniqueness with a straightforward index:

db.uniquestrings.createIndex( { "uniquestring" : 1 } , { unique : true } )

现在,您的应用程序在保存数据时需要做更多的工作(它需要保存到uniquestrings集合以及accounts集合中),但是您现在确实可以在数据库级执行这些操作的唯一性整个数据库中的字符串.

Now, your app will have a little more work to do when saving the data (it needs to save to the uniquestrings collection as well as the accounts collection), but you do now have database-level enforcement of the uniqueness of these strings, across the database.

欢迎任何对如何在猫鼬中实现和使用此类模型有更详细知识的人进行PS编辑.

PS edits are welcome from anybody with more detailed knowledge of how to implement and use such models in mongoose.

这篇关于在一个集合的所有文档上具有唯一值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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