我正在尝试设置我的测试数据库,插入多个角色项
...
// ADD 3 roles in first group
const roleA1 = { _groupId: groups[0]._id, name: 'admin', description: 'can RW group, user', users: [], permissions: permissions1 };
const roleA2 = { _groupId: groups[0]._id, name: 'manager', description: 'can R group, can RW user', users: [], permissions: permissions2 };
const roleA3 = { _groupId: groups[0]._id, name: 'employee', description: 'can R group, can read user', users: [], permissions: permissions3 };
// ADD 2 roles in second group
const roleB2 = { _groupId: groups[1]._id, name: 'manager', description: 'can R group, can RW user', users: [], permissions: permissions2 };
const roleB3 = { _groupId: groups[1]._id, name: 'employee', description: 'can R group, can read user', users: [], permissions: permissions3 };
// ADD 1 role in third group
const roleC3 = { _groupId: groups[2]._id, name: 'employee', description: 'can R group, can read user', users: [], permissions: permissions3 };
// ADD 1 role wo any group
const role4 = { name: 'visitor', description: 'no permissions', users: [], permissions: [] };
return Role.insertMany([roleA1, roleA2, roleA3, roleB2, roleB3, roleC3, role4 ])
.then((insertedRoles) => {
console.log('Successfully created test roles in groups');
return insertedRoles;
})
.catch((e) => {
console.log('Error inserting many: %j', e);
return e;
});
我在roleB2上获得了一个重复的密钥。 (名称'manager')...但名称字段未在角色模型中定义为唯一
/**
* Role Schema
*/
const RoleSchema = new Schema({
_groupId: { type: ObjectId, ref: 'Group'},
name: { type: String, required: true },
description: { type: String, trim: true, },
users: [{ type: ObjectId }, { _id: false }],
permissions: [PermissionSchema]
});
没有要设置的isNew标签.. ??怎么了?
感谢您的反馈
更新
mongoose debug的奇怪输出... roles.count重复... ???
groups[0]._id: 5954ee4d3b4027368ba66855
groups[0]._id: 5954ee4d3b4027368ba66856
groups[0]._id: 5954ee4d3b4027368ba66857
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66855") }, { name: 'admin' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66855") }, { name: 'admin' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66855") }, { name: 'manager' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66855") }, { name: 'manager' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66855") }, { name: 'employee' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66855") }, { name: 'employee' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66856") }, { name: 'manager' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66856") }, { name: 'manager' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66856") }, { name: 'employee' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66856") }, { name: 'employee' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66857") }, { name: 'employee' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: ObjectId("5954ee4d3b4027368ba66857") }, { name: 'employee' } ] }, {})
Mongoose: roles.count({ '$and': [ { _groupId: null }, { name: 'visitor' } ] }, {})