2
var addNewUser = function (id, chatId) {
    db.update({ _id: id }, { $push: { users: chatId } }, {}, function (err, numAffected) {
        // code after the record is updated
    });
}

in this code I want to push new Id to the array if it is not in it.

I've read the docs of neDB but it seems there is no way to do that

I'm beginner so I think there is a way to do that but I cant see it.

Raghav Garg
  • 3,601
  • 2
  • 23
  • 32
Engineer Passion
  • 1,051
  • 1
  • 8
  • 14

1 Answers1

1

To push new chatId to users array only if it does not exist, you can use $addToSet. According to the nedb document:

$addToSet adds an element to an array only if it isn't already in it

Here is the example code:

var addNewUser = function (id, chatId) {
  db.update({ _id: id }, { $addToSet: { users: chatId } }, {}, function (err, numAffected) {
    // code after the record is updated
  });
}
shaochuancs
  • 15,342
  • 3
  • 54
  • 62