如何使用nodejs将对象推入猫鼬现有集合的数组中?

时间:2020-07-28 14:36:39

标签: node.js mongodb express mongoose atom-editor

当用户在页面上使用除数组字段以外的所有信息进行注册时,将在mongoDB中创建用户集合。登录后,用户填写一张表格并提交数据。我想将这些数据存储在``用户''集合数组中,即您在架构中看到的Addtasks。如何实现?

var userSchema = new mongoose.Schema({
 email: {
    type: String,
    unique: true,
    required: true
  },
  name: {
    type: String,
    required: true
  },
  hash: String,
  salt: String,
  Addtasks : [{
    topic: String,
    words: Number,
    keywords: String,
    website: String,
    otherdetails: String,
    exampleRadios: String
 }]
});

userSchema.methods.setPassword = function(password) {
 this.salt = crypto.randomBytes(16).toString('hex');
 this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha1').toString('hex');
};


userSchema.methods.validPassword = function(password) {
  var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha1').toString('hex');
  return this.hash === hash;
};


module.exports = mongoose.model('User', userSchema);

0 个答案:

没有答案