【问题标题】:meteor js simpleschema Index with pattern already exists with different options带有模式的meteor js simpleschema索引已经存在不同的选项
【发布时间】:2016-03-12 18:27:37
【问题描述】:

我正在使用带有简单模式的流星 js 并得到 ​​p>

MongoError: Index with pattern: { username: 1 } already exists with different options

我围绕 db.users 集合的架构是

Schema = {};

Schema.User = new SimpleSchema({
...
 username: { 
     type: String, 
     unique: true, 
     regEx: /^[a-z0-9]{3,32}$/ , 
     max: 32,
     min: 3 },
...
});

我已经在 mongodb 中删除了索引,但是当我重新启动我的应用程序时仍然出现错误。有人遇到过这个吗?

【问题讨论】:

  • 顺便说一句,为什么要再次设置最小值和最大值?正则表达式可能没问题..并且您可能也想包含 A-Z (或使其不区分大小写:/^[a-z0-9]{3,32}$/i 另外..让用户名是个好主意以数字开头?你决定;)

标签: javascript mongodb meteor


【解决方案1】:

Meteor 在users 集合上带有一些默认索引。来自accounts-base

/// DEFAULT INDEXES ON USERS
Meteor.users._ensureIndex('username', {unique: 1, sparse: 1});
Meteor.users._ensureIndex('emails.address', {unique: 1, sparse: 1});
Meteor.users._ensureIndex('services.resume.loginTokens.hashedToken',
                          {unique: 1, sparse: 1});
Meteor.users._ensureIndex('services.resume.loginTokens.token',
                          {unique: 1, sparse: 1});
// For taking care of logoutOtherClients calls that crashed before the tokens
// were deleted.
Meteor.users._ensureIndex('services.resume.haveLoginTokensToDelete',
                          { sparse: 1 });
// For expiring login tokens
Meteor.users._ensureIndex("services.resume.loginTokens.when", { sparse: 1 });

我没有检查,但我猜unique: true 可能是您的架构中与上述冲突的部分。

【讨论】:

    【解决方案2】:

    您可以将您的架构与已为用户名和电子邮件设置索引和唯一性的现有架构完美地合并。因为您只能通过将它们明确设置为 false 来删除它们。

    【讨论】:

    • 因此,如果您只想将您的正则表达式添加到用户名中,您可以在没有合并选项的情况下附加(没有索引或已定义的唯一性)...
    • 你能举个例子吗,我总是得到“已经用不同选项错误定义的索引”,即使我先放弃它
    猜你喜欢
    • 2017-04-30
    • 2019-01-11
    • 2020-06-01
    • 2015-07-25
    • 2020-01-06
    • 1970-01-01
    • 2019-03-19
    • 2016-02-07
    • 1970-01-01
    相关资源
    最近更新 更多