Mongoose,唯一的用户名,但不是必需的(Mongo)

时间:2015-09-07 18:20:08

标签: node.js mongodb mongoose

我不知道这是否可行,搜索过的文档却无法找到答案。

我有一个方案,用户使用Facebook ID注册,并提供全名和身份证。

稍后在旅途中,用户可以注册多人游戏部分并选择一个如此定义的唯一用户名:

//In >> var UserSchema = new mongoose.Schema
multiplayer.username: {type: String, unique: true, required: false}

我的问题是,当我向新用户尝试此操作时,它只是吐出错误:

New user request: Superman, facebookid: 13131231222
{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: gameserver.users.$multiplayer.username_1  dup key: { : null }]
  name: 'MongoError',
  message: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: gameserver.users.$multiplayer.username_1  dup key: { : null }',

我尝试过使用默认设置,但它不起作用,我不能使用随机数据,因为它是唯一的,不可重置。

有什么想法吗?

修改

我查看了http://stackoverflow.com/questions/7955040/mongodb-mongoose-unique-if-not-null

并且它不起作用,同样的错误。

0 个答案:

没有答案