Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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
Sql 如何将此mongo查询移植到postgres?_Sql_Mongodb_Postgresql - Fatal编程技术网

Sql 如何将此mongo查询移植到postgres?

Sql 如何将此mongo查询移植到postgres?,sql,mongodb,postgresql,Sql,Mongodb,Postgresql,我有一个原子更新,它按属性中的最低值查找记录,增加该值,并在一个查询中返回结果 如果我在SQL Server中,我会在事务中使用DECLARE和SET,并执行多个查询,但这种postgres do语法对我来说是煎饼 我看到DO$$DECLARE r记录这是一个好的开始,但我可以设置或通过其他方式将单个记录分配给该变量吗 在蒙哥: this.findOneAndUpdate( { active: true }, { $inc: { userCount: 1 } }, { s

我有一个原子更新,它按属性中的最低值查找记录,增加该值,并在一个查询中返回结果

如果我在SQL Server中,我会在事务中使用DECLARE和SET,并执行多个查询,但这种postgres do语法对我来说是煎饼

我看到
DO$$DECLARE r记录这是一个好的开始,但我可以设置或通过其他方式将单个记录分配给该变量吗

在蒙哥:

this.findOneAndUpdate(
    { active: true },
    { $inc: { userCount: 1 } },
    { sort: { userCount: 1 } }, // grab the lowest count for update
    cb
);
在SQL Server中,我会这样做:(伙计,已经有一段时间了…)


不需要do语句。。。将普通update语句与returning子句一起使用,例如:

update foos 
set userCount = userCount + 1 
where id = (
      select id 
      from foos 
      where active = 1 
      order by userCount desc
      limit 1
    )
returning *
update foos 
set userCount = userCount + 1 
where id = (
      select id 
      from foos 
      where active = 1 
      order by userCount desc
      limit 1
    )
returning *