I'm getting usersfrom this API.
https://redmine-mock-api.herokuapp.com/api/v1/users
There are 100 in total (100 different ids). I added them to my mongodb database using mongoose but the problem is that it's always adding (i already have 5000 users (50 times repeated 100 users)
I want to add if the id does not exist or update if it exists.
What am I doing wrong? users is the array of users from the API
db.collection("users").insertMany(users, function (error, response) {
if (error) throw error;
console.log("Number of users inserted: " + response.insertedCount);
db.close();
});
try following format
db.collection.update({'_id': Id}, //Find with the unique identifier
{//put whatever you want to insert},
{upsert: true}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With