0

User collection is created in mongoDB when user sign up on page with all the information except array fields. After login the user fills one form and submit the data. Those datas I want to store in 'User' collection array i.e. Addtasks as you can see in schema. How to achieve that?

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);
Dhruv Shah
  • 1,611
  • 1
  • 8
  • 22
Vikas Yadav
  • 13
  • 1
  • 5
  • User collection is created in mongoDB when user sign up on page with all the information except array fields. After login the user fills one form and submit the data. Those datas I want to store in 'User' collection array i.e. Addtasks as you can see in schema. How to achieve that? – Vikas Yadav Jul 28 '20 at 14:37
  • 1
    Same exact type of question: https://stackoverflow.com/questions/33049707/push-items-into-mongo-array-via-mongoose. – Prince Agrawal Jul 28 '20 at 14:43
  • So in this link's solution: exports.addFriend = function (req, res, next) { var friend = {"firstName": req.body.fName, "lastName": req.body.lName}; Users.findOneAndUpdate({name: req.user.name}, {$push: {friends: friend}}); }; Can you tell me how req.user.name is being fteched? what is user here? – Vikas Yadav Jul 28 '20 at 15:52
  • They are pushing "friend" object in "friends" array. You can do the same with "Addtasks". In your case "User" is the collection and they have "people". – Prince Agrawal Jul 28 '20 at 17:09

0 Answers0