![](/img/trans.png)
[英]How to update multiple objects of an array in a single mongo db update operation?
[英]How to update and post in single operation in mongodb
var postStop = async (machine,stop_name,timestamp)=>{
var stop = new Stop({
machine_name:machine,
stop_name:stop_name,
start_time:timestamp,
end_time:null
});
stop.save();
}
var updateStop = async (machine,stop_name,start_time,end_time)=>{
var stop = await Stop.findOne({machine_name:machine,stop_name:stop_name,start_time:start_time});
if(!stop){
console.log(machine,stop_name,start_time,end_time)
}
stop.end_time = end_time;
stop.save();
}
我必須在具有某些條件的單個操作中更新和發布。
我已將這兩者合並為一個如何執行此操作。
如果您希望在同一操作中更新或創建,則可以使用upsert 選項
var stop = await Stop.update(
{machine_name:machine,stop_name:stop_name,start_time:start_time},
{end_time:end_time},
{upsert: true});
可以選擇在單個查詢中插入文檔(如果不存在)和更新(如果存在)。
語法:db.collection.update(query, update, {upsert: true})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.