我在udids
架构中有一个名为Meteor.users
的数组字段,该字段应包含唯一元素。这就是我使用SimpleSchema和Collection2定义索引的方法:
new SimpleSchema({
...
udids: {
type: Array,
index: true,
unique: true,
optional: true,
sparse: true,
},
'udids.$': {
type: String,
},
...
})
但是,当我启动应用时,我收到了此错误:E11000 duplicate key error collection: meteor.users index: c2_udids dup key: { : undefined }
我尝试在数据库中使用udids = undefined
搜索文档:db.users.find({ udids: { $type: 6 } })
($type: 6
为undefined
值,但它不返回任何内容。
答案 0 :(得分:0)
我没有对此进行测试,但理想情况下应该可行。
Meteor.users
作为集合名称。您可能希望将其替换为要对其运行验证的任何集合。 new SimpleSchema({
...
'udids': {
optional: true,
type: [String], // an array of string eg. ['A','B']
custom: function() {
// this.value = current field's value
// Below mongo query will check to see if there is at least one
// mongo doc whose udids contains the currently entered field's value.
// if found, then return the error message.
if (Meteor.users.findOne({
udids: {
$in: [this.value]
}
})) {
return "duplicateMsg";
}
},
...
});
SimpleSchema.messages({ duplicateMsg:"Udid already exists"});
答案 1 :(得分:0)
错误信息有点不清楚所以我不得不猜测原因。我发现当前数据库已经有一些用户udids = []
。我正在编写一个迁移脚本来取消这些用户的这个字段。希望这能帮助那些和我一样有问题的人。