0

MongoDB Data object is here, It use mongoose so has Schema, The Schema name is Users :

{ 
  id: abcd1234
  level: 1,
  user_name: 'Man'
}

I want to make general function that can change database.

function editData(key, value){
    Users.findOne({ id : 'abcd1234' }, function(err, user){
        // The problem is below line 
        user.key = value; 

        user.save(....);
    });
}

It didn't work. because user doesn't has key schema. I think Node.js think the 'key' to mongodb schema, not a parameter. How can I use key as a parameter?

ton1
  • 7,238
  • 18
  • 71
  • 126
  • 1
    You are doing it wrong anyway. Do not `.findOne()` and `.save(). There is no guarantee the document does not change on the server in the middle of that process. Use [`.findByIdAndUpdate()`](http://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate) instead – Blakes Seven Apr 02 '16 at 06:41
  • @BlakesSeven Thanks. It works well following document that you noticed. – ton1 Apr 02 '16 at 06:44
  • @BlakesSeven Maybe I should use findByIdAndUpdate. It just sample code but I didn't know that. – ton1 Apr 02 '16 at 06:45

0 Answers0