mongo数组内唯一索引的问题
2016-11-10 10:07
253 查看
之前一直认为通过唯一唯一索引可以确保doc数组字段的每个doc确保唯一(根据某个字段)如:
staffs: [staffRoleSchema],
后来查了文档,其实在 http://stackoverflow.com/questions/15921700/mongoose-unique-values-in-nested-array-of-objects
30
down vote
A unique index on an array field enforces that the same value cannot appear in the arrays of more than one document in 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 $addToSet operator to add a value to an array only if the value is not already present.
Group.update({name: 'admin'}, {$addToSet: {users: userOid}}, ...
However, if the users array contains objects with multiple properties and you want to ensure uniqueness over just one of them (uid in this case), then you need to take another approach:
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 $push update to only occur if user.uid doesn't already exist in the uid field of any of the elements of users. So it mimics $addToSet behavior, but for just uid.
staffs: [staffRoleSchema],
const staffRoleSchema = new Schema({ openid: { type: String, index: { unique: true }, sparse: true }, privilege: { type: String, enum: ['general', 'manage', 'insider'] }, }, { _id: false });
后来查了文档,其实在 http://stackoverflow.com/questions/15921700/mongoose-unique-values-in-nested-array-of-objects
30
down vote
A unique index on an array field enforces that the same value cannot appear in the arrays of more than one document in 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 $addToSet operator to add a value to an array only if the value is not already present.
Group.update({name: 'admin'}, {$addToSet: {users: userOid}}, ...
However, if the users array contains objects with multiple properties and you want to ensure uniqueness over just one of them (uid in this case), then you need to take another approach:
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 $push update to only occur if user.uid doesn't already exist in the uid field of any of the elements of users. So it mimics $addToSet behavior, but for just uid.
相关文章推荐
- mongo3.2 数组索引作为联合索引的一部分导致的范围查询问题
- Swift教程_swift常见问题(0006)_完美解决swift数组索引是变量时的内存溢出
- mysql 插入去重,以及设置联合唯一索引问题
- 关于数组索引顺序以及关联数组下标问题
- 创建唯一索引的时候遇到问题,提示“唯一索引的分区依据列必须是索引键的子集”
- mongo两级数组嵌套数据更新问题
- 完美解决thinkphp唯一索引重复时出错的问题
- Go数组中的索引问题
- Oracle创建唯一索引遇到的问题(ORA-01452: 无法 CREATE UNIQUE INDEX; 找到重复的关键字)
- .net reflector 反编译失败 索引超出了数组界限问题处理方法
- 连续子数组最大和问题(能够处理全是负数,返回子数组的起止索引的O(N)算法)
- mongo 索引问题
- mongo集合以_id作为分片键的,不能在其他列上创建唯一索引
- 如何解决Oracle“不能创建唯一索引,发现重复记录”问题
- mongo清洗id不唯一的脏数据,并创建唯一索引
- 如何解决Oracle“不能创建唯一索引,发现重复记录”问题
- 关于oracle导入sqlserver存在的唯一索引问题
- 关于oracle导入sqlserver存在的唯一索引问题
- mongodb指南(十八) - developer zone - 索引(二)_id索引、复合索引、稀疏索引、数组索引、唯一索引
- mongodb指南(翻译)(十八) - developer zone - 索引(二)_id索引、复合索引、稀疏索引、数组索引、唯一索引