I have a collection called Users.
Here is an example of a doc.
{"_id":{"$oid":"xxxxxxxxxxxxxxxxxxx"},
"userId":"ANKIT",
"token":"token123",
"badge":{"$numberInt":"0"},
"useLocalCurrency":true,
"notifyCustomerRejected":true,
"notifyReworkRequest":true,
"notifyMoaApproved":true,
"notifyCustomerAccepted":true,
"__v":{"$numberInt":"0"},
"tokens":[]}
I am trying to push the token
into the tokens
array for all the docs in a DB migration.
This is what I have tried :
export function up(next) {
let mClient = null;
return MongoClient.connect(url)
.then(client => {
mClient = client;
return client.db('notifications');
})
.then(db => {
const User = db.collection('users');
return User.find().forEach(result => {
let { _id, userId, tokens, token } = result;
tokens.push(token);
tokens = Array.from(new Set(tokens));
result.tokens = tokens;
console.log(result._id);
console.log(result.tokens);
User.update({ _id: _id, userId: userId }, { $set: { tokens: tokens } });
});
})
.then(() => {
mClient.close();
return next();
})
.catch(err => next(err));
}
By doing this I am only getting the 1st document updated the way I want and not the rest. What am I doing wrong here?
Thanks