Is There A Way To Prevent Duplication Of Entries In NeDB Collection's Array?
var addNewUser = function (id, chatId) { db.update({ _id: id }, { $push: { users: chatId } }, {}, function (err, numAffected) { // code after the record is updated
Solution 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
});
}
Post a Comment for "Is There A Way To Prevent Duplication Of Entries In NeDB Collection's Array?"