mongodb中的sotre数据通过mongoose模式,不传递唯一真键值

时间:2022-10-17 04:18:27

i have a mongoose schema like this :-

我有一个像这样的蒙古图式:-

var UserSchema = new Schema({
name: {type: String, required: true},
email: {type: String, required: true,unique: true},
mobNum: {type: String, required: true, unique: true},
username: {type: String, unique: true},
dob:{ type: Date},
isTempUser: {type: String, default: true},
mobNumVerified: {type: Boolean, default: false},
emailVerified: {type: Boolean, default: false},
registrationComplete: {type: Boolean, default: false}})

When i try to save data in db with this code:-

当我尝试用这个代码在db中保存数据:-

                    let newUser = User();
                    newUser.name = name;
                    newUser.email = email;
                    newUser.mobNum = mobNum;
                    newUser.save();

when i run server and hit my api for register new user only frist time data save successfully in mongodb when i try different value save in db i got and error

当我运行服务器并为注册新用户点击api时,当我尝试在db中保存不同的值时,我得到了错误

that's :-

这就是:

{ MongoError: E11000 duplicate key error collection: okhlee-kdb-promoter.users index: username_1 dup key: { : null }
    at Function.MongoError.create (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb-core\lib\error.js:31:11)
    at toError (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\utils.js:139:22)
    at D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\collection.js:659:23
    at handleCallback (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\utils.js:120:56)
    at D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\bulk\unordered.js:465:9
    at handleCallback (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\utils.js:120:56)
    at resultHandler (D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb\lib\bulk\unordered.js:413:5)
    at D:\Okhlee.com\KDB Promoter backend\node_modules\mongodb-core\lib\connection\pool.js:461:18
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
  name: 'MongoError',
  message: 'E11000 duplicate key error collection: okhlee-kdb-promoter.users index: username_1 dup key: { : null }',
  driver: true,
  code: 11000,
  index: 0,
  errmsg: 'E11000 duplicate key error collection: okhlee-kdb-promoter.users index: username_1 dup key: { : null }',
  getOperation: [Function],
  toJSON: [Function],
  toString: [Function] }

As i know this error is related to duplicate value of username but i when save data in db i'm not store username on frist api i save username next time when user come on our webpage .

我知道这个错误与用户名的重复值有关,但是当我在db中保存数据时,我没有将用户名存储在第一个api上,下次当用户访问我们的网页时,我会保存用户名。

So please solve this error.

请解决这个错误。

1 个解决方案

#1


2  

In MongoDB a unique field can not be empty in multiple records. If your user will not provide username at first but you still need the field to be unique, then you can store a random string or mobile num or email as username in the first attempt. I think that will work for you.

在MongoDB中,唯一字段不能在多个记录中为空。如果您的用户最初不提供用户名,但您仍然需要该字段是惟一的,那么您可以在第一次尝试中存储一个随机字符串或移动num或电子邮件作为用户名。我想这对你是行得通的。

If you wish to create a random string and assign it to username, you can try the following using crypto:

如果您希望创建一个随机字符串并将其分配给用户名,您可以使用crypto尝试以下操作:

  let newUser = User();
  newUser.name = name;
  newUser.email = email;
  newUser.mobNum = mobNum;
  require('crypto').randomBytes(48, function(err, buffer) {
        if(err) { throw err }
        username = buffer.toString('hex');
        newUser  = username;
        newUser.save();
  });

#1


2  

In MongoDB a unique field can not be empty in multiple records. If your user will not provide username at first but you still need the field to be unique, then you can store a random string or mobile num or email as username in the first attempt. I think that will work for you.

在MongoDB中,唯一字段不能在多个记录中为空。如果您的用户最初不提供用户名,但您仍然需要该字段是惟一的,那么您可以在第一次尝试中存储一个随机字符串或移动num或电子邮件作为用户名。我想这对你是行得通的。

If you wish to create a random string and assign it to username, you can try the following using crypto:

如果您希望创建一个随机字符串并将其分配给用户名,您可以使用crypto尝试以下操作:

  let newUser = User();
  newUser.name = name;
  newUser.email = email;
  newUser.mobNum = mobNum;
  require('crypto').randomBytes(48, function(err, buffer) {
        if(err) { throw err }
        username = buffer.toString('hex');
        newUser  = username;
        newUser.save();
  });