我正在使用这个API获取用户信息。
https://redmine-mock-api.herokuapp.com/api/v1/users
总共有100个用户(100个不同的id)。 我使用mongoose将它们添加到了我的mongodb数据库中,但问题是它总是重复添加(我已经有5000个用户(50次重复100个用户))。
如果id
不存在,我想添加它,如果存在则更新它。
我做错了什么?users
是从API返回的用户数组。
db.collection("users").insertMany(users, function (error, response) {
if (error) throw error;
console.log("Number of users inserted: " + response.insertedCount);
db.close();
});
upsert
标志。尝试搜索mongodb upsert
。 - Wyck