Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在mongodb中如何在单个操作中更新和发布_Javascript_Node.js_Database_Mongodb_Mongoose - Fatal编程技术网

Javascript 在mongodb中如何在单个操作中更新和发布

Javascript 在mongodb中如何在单个操作中更新和发布,javascript,node.js,database,mongodb,mongoose,Javascript,Node.js,Database,Mongodb,Mongoose,我必须在一次操作中更新和发布某些条件。 我已经将这两种方法合并为一种方法。如果您希望在相同的操作中进行更新或创建,您可以使用 在单个查询中可以选择插入文档(如果不存在)和更新(如果存在) 语法: db.collection.update(query,update,{upsert:true})实际上,感谢您的回复,我正在创建一个时间间隔系列数据库,其中每个文档都有开始时间和结束时间,对于每个新文档插入,首先我必须更新最后插入的值结束时间戳,该时间戳是下一个插入文档的开始时间。实际上,感谢您的回复,

我必须在一次操作中更新和发布某些条件。

我已经将这两种方法合并为一种方法。

如果您希望在相同的操作中进行更新或创建,您可以使用


在单个查询中可以选择插入文档(如果不存在)和更新(如果存在)

语法:
db.collection.update(query,update,{upsert:true})

实际上,感谢您的回复,我正在创建一个时间间隔系列数据库,其中每个文档都有开始时间和结束时间,对于每个新文档插入,首先我必须更新最后插入的值结束时间戳,该时间戳是下一个插入文档的开始时间。实际上,感谢您的回复,我正在创建一个时间间隔序列数据库,其中每个文档都有开始时间和结束时间,对于每个新文档插入,首先我必须更新最后插入的值结束时间戳,该时间戳是下一个插入文档的开始时间
    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();
}
var stop = await Stop.update(
    {machine_name:machine,stop_name:stop_name,start_time:start_time},
    {end_time:end_time},
    {upsert: true});