node.js Mongoose 嵌套对象数组中的唯一值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15921700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose Unique values in nested array of objects
提问by Guido Passage
For my project, I want to keep a mongoose document for groups of organizations, like this:
对于我的项目,我想为组织组保留一个猫鼬文档,如下所示:
var groupSchema = Schema({
name : { type : String },
org : { type : Schema.Types.ObjectId, ref : 'Organization' },
...
users : [{
uid : { type : Schema.Types.ObjectId, ref : 'User' },
...
}]
});
I want to prevent the same user from being in the same group twice. To do this, I need to force users.uid to be unique in the users array. I tried stating 'unique : true' for uid, but that didn't work. Is there a way to do this with mongoose or mongoDB without extra queries or splitting the schema?
我想防止同一个用户两次出现在同一个组中。为此,我需要强制 users.uid 在 users 数组中是唯一的。我尝试为 uid 声明“unique : true”,但这没有用。有没有办法用 mongoose 或 mongoDB 做到这一点,而无需额外的查询或拆分架构?
Edit: I changed the previous value of uid to uid : { type : Schema.Types.ObjectId, ref : 'User', index: {unique: true, dropDups: true} } But this still doesn't seem to work.
编辑:我将之前的 uid 值更改为 uid : { type : Schema.Types.ObjectId, ref : 'User', index: {unique: true, dropDups: true} } 但这似乎仍然不起作用。
Edit: Assuming there is no simple way to achieve this, I added an extra query checking if the user is already in the group. This seems to me the simplest way.
编辑:假设没有简单的方法来实现这一点,我添加了一个额外的查询,检查用户是否已经在组中。这在我看来是最简单的方法。
回答by JohnnyHK
A unique index on an array field enforces that the same value cannot appear in the arrays of more than one documentin the collection, but doesn't prevent the same value from appearing more than once in a single document's array. So you need to ensure uniqueness as you add elements to the array instead.
在一个阵列场强制执行相同的值不能出现在一个以上的阵列唯一索引文件的集合中,但并不妨碍在单个文档的阵列出现不止一次相同的值。因此,您需要在向数组添加元素时确保唯一性。
Use the $addToSetoperator to add a value to an array only if the value is not already present.
$addToSet仅当值不存在时,才使用运算符将值添加到数组中。
Group.update({name: 'admin'}, {$addToSet: {users: userOid}}, ...
However, if the usersarray contains objects with multiple properties and you want to ensure uniqueness over just one of them (uidin this case), then you need to take another approach:
但是,如果users数组包含具有多个属性的对象,并且您想确保其中一个(uid在本例中)的唯一性,那么您需要采用另一种方法:
var user = { uid: userOid, ... };
Group.update(
{name: 'admin', 'users.uid': {$ne: user.uid}},
{$push: {users: user}},
function(err, numAffected) { ... });
What that does is qualify the $pushupdate to only occur if user.uiddoesn't already exist in the uidfield of any of the elements of users. So it mimics $addToSetbehavior, but for just uid.
,做什么是晋级的$push只有出现更新,如果user.uid没有已经在存在uid的任何元素的领域users。所以它模仿$addToSet行为,但只是为了uid.
回答by Ankit Balyan
Well this might be old question but for mongoose > v4.1, you can use $addToSetoperator.
好吧,这可能是个老问题,但是对于 mongoose > v4.1,您可以使用$addToSet运算符。
The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.
$addToSet 运算符向数组添加一个值,除非该值已经存在,在这种情况下 $addToSet 对该数组不执行任何操作。
example:
例子:
MyModal.update(
{ _id: 1 },
{ $addToSet: {letters: [ "c", "d" ] } }
)

