2017-08-07 26 views
1

我想使用mongoose的findByIdAndUpdate方法傳入對象標識列表並一次更新它們。但是,我收到「錯誤:無法使用ObjectId設置$。」錯誤,我似乎無法與我的代碼相關聯。通過傳遞列表objectids更新多個文檔findByIdAndUpdate

這是我的代碼。

return ComponentModel.findByIdAndUpdate({ 
    _id: { 
     $in: input.subjectIds 
    }, 
    $set: { location: input.newLocation } 
}).then(res => res); 

回答

1

findByIdAndUpdate是一個文件。對於多個文檔,您可以使用update並將multi標記爲true。

return ComponentModel.update(
{_id: {$in: input.subjectIds}}, 
{$set: {location: input.newLocation}}, 
{"multi": true} 
).then(res => res);