Search code examples
node.jsnedb

NeDB updates but removes fields that wasn't updated, why?


I am writing a little script for discord in node.js and using NeDB as a database but when I update a row then it does the update but deletes the field "userid".

db.update({userid: "u"+uid}, {credits: credit, dailyTime: currentTime}, {}, function(err, numReplaced){
        if(err) console.log(err);
        message.channel.send("<@"+uid+"> You received your daily credit! Your now have "+credit+" Credit!");
      });

This is my update code and when I get all the users an object now has the credit, dailyTime and _id but not the userid. Why it gets removed?

P.S: I searched google already but there were only questions where update is not working but for me it is working just not as I expected.


Solution

  • I guess you forgot specify $set modifier, which says to change a field's value. Try something like this:

    db.update({userid: "u"+uid}, {$set:{credits: credit, dailyTime: currentTime}}, {}, function(err, numReplaced){
            if(err) console.log(err);
            message.channel.send("<@"+uid+"> You received your daily credit! Your now have "+credit+" Credit!");
          });
    

    Please, look at official docs about updating documents