如何将带有exist的sql查询转换为mongodb查询

如何将带有exist的sql查询转换为mongodb查询,sql,mongodb,mongoose,mongodb-query,aggregation-framework,Sql,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有两个关于mongodb的文档,它们是百分比和项目。我擅长SQL,我可以编写如下PLSql查询,但我不能转换为mongodb查询。因为我的mongodb知识水平还处于初级阶段事实上,我知道我必须使用$gt作为和条件。但我不知道如何才能说mongodb不存在或联合关键字。如何编写mongodb查询?我应该搜索哪些关键字 select p.*, "to_top" as list from percentages p where p.percentage > 5 and p.up

我有两个关于mongodb的文档,它们是百分比项目。我擅长SQL,我可以编写如下PLSql查询,但我不能转换为mongodb查询。因为我的mongodb知识水平还处于初级阶段

事实上,我知道我必须使用$gt作为和条件。但我不知道如何才能说mongodb不存在或联合关键字。如何编写mongodb查询?我应该搜索哪些关键字

select p.*, "to_top" as list 
  from percentages p
 where p.percentage > 5
   and p.updatetime > sysdate - 1/24
   and not exists (select 1
                     from items i
                    where i.id = p.p_id
                      and i.seller = p.seller)
 order by p.percentage desc
union
select p2.*, "to_bottom" as list 
  from percentages p2
 where p2.percentage > 5
   and p2.updatetime > sysdate - 1/24
   and exists (select 1
                  from items i2
                 where i2.id = p2.p_id
                   and i2.seller = p2.seller)
order by p2.percentage desc

MongoDB没有
联合
。幸运的是,每个查询都在同一个集合上执行,并且条件非常接近,因此我们可以实现“Mongo way”查询

解释 通常,所有复杂的SQL查询都是使用框架完成的

  • 我们按
    百分比
    /
    更新时间
    过滤文档。为什么我们需要使用
    $expr
  • SQL联接/子查询是使用运算符完成的
  • MongoDB方式下的SQL
    SYSDATE
    可以是
    NOW
    CLUSTER\u TIME

  • 注意:MongoDB聚合具有允许对同一集合执行不同查询的运算符

    模式:

    db.percentages.aggregate([
      {$facet:{
        q1:[...],
        q2:[...],
      }},
      //We apply "UNION" the result documents for each pipeline into single array
      {$project:{
        data:{$concatArrays:["$q1","$q2"]}
      }},
      //Flatten array into single object
      {$unwind:"$data"}
      //Replace top-level document
      {$replaceWith:"$data"}
    ])
    

    为什么不将mangoDB数据导入oracle并使用sql(比mango更简单、更强大)。

    您的集合是什么样子的,以及您想要的相应输出是什么?是的,oracle对我来说更容易,但我认为mongodb会产生更快的结果。我的nodejs应用程序必须更快。我也想学习mongo。
    db.percentages.aggregate([
      {$facet:{
        q1:[...],
        q2:[...],
      }},
      //We apply "UNION" the result documents for each pipeline into single array
      {$project:{
        data:{$concatArrays:["$q1","$q2"]}
      }},
      //Flatten array into single object
      {$unwind:"$data"}
      //Replace top-level document
      {$replaceWith:"$data"}
    ])