[英]Returning list of search values that were not updated (Mongoose, updateMany)
使用 Mongoose 的updateMany
function,我可以提供一個搜索列表來查找用戶並進行相應更新。
const searchList = ["userA", "userB", "UNKNOWNUSER"];
const updatedRecords = await User.updateMany({username: {$in: searchList}}, {foo: "bar"});
使用nmodified
可以輕松找到結果的數量。 但是如果有一個用戶找不到,顯然在這種情況下它不會被更新。 除了一些丑陋BulkWrite
或同步循環功能外,不完全確定如何在一輪 ZCCADCDEDB567ABAE643E15DCF0974E503Z 行程中找到無法找到或更新的 searchList 中的元素。
我知道這個答案與您要求的批量插入無關,但它可能會有所幫助。 如果您想獲取現有數據以供插入,您可以在此處使用 for 循環:
const searchList = ["userA", "userB", "UNKNOWNUSER"];
for(user of searchList){
const updated = await User.findOneAndUpdate({username: user}, {$set:{foo: "bar"}}, {new : true});
if(updated){
// that means it would be found
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.