【问题标题】:How to convert sql query with exist into mongodb query如何将sql查询转换为mongodb查询中的exists
【发布时间】:2020-06-14 09:00:29
【问题描述】:

我在 mongodb 上有两个文档,分别是 percentagesitems。我擅长 SQL,我可以编写如下 PLSql 查询但我无法转换为 mongodb 查询。因为我的mongodb知识水平还处于起步阶段。

实际上,我知道我必须将 $gt 用于 and 条件。但我不知道我怎么能说 mongodb 的不存在或 union 关键字。如何编写 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

【问题讨论】:

  • 你的收藏看起来像什么,你想要的对应输出是什么?

标签: sql mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

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

说明

通常,几乎所有复杂的 SQL 查询都使用MongoDB aggregation 框架完成。

  1. 我们通过percentage/updatetime过滤文档。 Explanation 为什么我们需要使用$expr
  2. SQL JOIN / 子查询使用$lookup 运算符完成。
  3. MongoDB 方式中的 SQL SYSDATE 可以是 NOWCLUSTER_TIME variable

db.percentages.aggregate([
  {
    $match: {
      percentage: { $gt: 5 },
      $expr: {
        $gt: [
          "$updatetime",
          {
            $subtract: [
              ISODate("2020-06-14T13:00:00Z"), //Change to $$NOW or $$CLUSTER_TIME
              3600000
            ]
          }
        ]
      }
    }
  },
  {
    $lookup: {
      from: "items",
      let: {
        p_id: "$p_id",
        seller: "$seller"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [ "$$p_id", "$id"]
                },
                {
                  $eq: [ "$$seller", "$seller"]
                }
              ]
            }
          }
        },
        {
          $limit: 1
        }
      ],
      as: "items"
    }
  },
  {
    $addFields: {
      list: {
        $cond: [
          {
            $eq: [{$size: "$items"}, 0]
          },
          "$to_top",
          "$to_bottom"
        ]
      },
      items: "$$REMOVE"
    }
  },
  {
    $sort: { percentage: -1 }
  }
])

MongoPlayground

注意: MongoDB 聚合具有$facet 运算符,允许对同一个集合执行不同的查询。

架构:

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"}
])

MongoPlayground

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,oracle 对我来说更容易,但我认为 mongodb 会产生更快的结果。我的 nodejs 应用程序必须更快。我也想学习mongo。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多