【问题标题】:SQL Query after migrate from PostgreSQL to MongoDB从 PostgreSQL 迁移到 MongoDB 后的 SQL 查询
【发布时间】:2021-08-21 03:12:28
【问题描述】:

最近,我将我的 postgreSQL 数据库迁移到了 MongoDB。假设我在 PostgreSQL 中有 2 个表并迁移成为 MongoDB 中的 2 个集合。 在 PostgreSQL 中:

  1. 表账号代码

    ac_id desc group_id p_code c_code
    999.01 PettyCash 9 42000 99000
    888.09 Tax 34 70000 66000
    999.02 PC2 9 43000 99000
  2. 表事务

    reffNum ac_id db cr desc dateTime id audited
    GL/0003845/V/11/21/XXX 999.01 950000 0 Transfer from Client 2021-03-17 09:58:41.304302 2 0
    GL/0005584/V/11/21/XXX 999.02 10000 0 Transfer from Client 2021-03-18 09:58:41.304302 2 0
    SL/00077898/V/11/21/XXX 888.09 997500 0 Tax 2021-03-18 09:58:41.304302 2 0
    GL/0005485/V/11/21/XXX 999.01 0 150000 Transfer to Client 2021-03-25 09:58:41.304302 3 0

我有这样的 SQL 查询:

SELECT CASE WHEN (SUM (A .db) - SUM (A .cr)) is NULL  THEN '0' ELSE (SUM (A .db) - SUM (A .cr)) END from transaction a, accountCode b
where a.ac_id = b.ac_id and a.id<>0 AND (a.audited='0') AND to_char(a.dateTime,'YYYY-MM') = '2021-03' AND b.groupid="9"

它应该给出结果:810000

我将数据库迁移到 MongoDB 中的 2 个集合,类似于 PostgreSQL 中的 2 个表。但是现在,我很困惑如何查询,所以我在 MongoDB 中有相同的结果?或者我应该将结构从 2 个表更改为 MongoDB 中的 1 个单个集合?任何想法? TIA。

【问题讨论】:

    标签: mongodb postgresql select where-clause


    【解决方案1】:

    您可以使用 $lookup(Aggregation) 连接两个集合

    {$lookup:
     {
       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }}
    

    但建议将这两个表创建为一个集合,Mongodb 就是这样使用的。

    【讨论】:

      猜你喜欢
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 2017-06-19
      • 2013-09-18
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多