【问题标题】:MongoDB Aggregation SQL Union and SQL Exists like clauseMongoDB Aggregation SQL Union 和 SQL Exists like 子句
【发布时间】:2015-06-02 21:32:01
【问题描述】:

我想对这样的数据进行 MongoDB 聚合查询:

集合 A

{
_id : 1,
Active : true,
hasQuery : false,
Running : false,
}

{
_id : 2,
Active : true,
hasQuery : true,
Running : false,
}

{
_id : 3,
Active : true,
hasQuery : false,
Running : true,
}

{
_id : 4,
Active : true,
hasQuery : true,
Running : true,
}

{
_id : 5,
Active : false,
hasQuery : false,
Running : false,
}

{
_id : 6,
Active : false,
hasQuery : false,
Running : true,
}

这个 JSON 数据可以用这样的表结构来表示 表 A

 PrimaryKey   |    Active    |    hasQuery    |       Running

  1           |     true     |    false       |        false

  2           |     true     |    true        |        false

  3           |     true     |    false       |        true

  4           |     true     |     true       |        true

  5           |     false    |     false      |        false

  6           |    false     |     false      |        true

如果我在表上应用以下查询:

select * from A where Exists(Select * from A where A.Running=true and A.hasQuery=true) and A.Running=false and A.hasQuery=false and A.Active=true

union

select * from A where not Exists(Select * from A where A.Running=true and A.hasQuery=true) and A.Running=false and A.Active=true

我得到了这些结果: 在 MongoDB 中:

{
_id : 1,
Active : true,
hasQuery : false,
Running : false,
}

{
_id : 2,
Active : true,
hasQuery : true,
Running : false,
}

{
_id : 5,
Active : false,
hasQuery : false,
Running : false,
}

在 SQL 中:

 PrimaryKey   |    Active    |    hasQuery    |       Running

  1           |     true     |    false       |        false

  2           |     true     |    true        |        false

  5           |     false    |     false      |        false

如何用 mongoDB 聚合做同样的事情?

【问题讨论】:

    标签: sql mongodb exists


    【解决方案1】:

    我设法用那个代码做到了:

    db.test.aggregate(
        {
             $project:
               {
                   RunningActiveRecordHasQuery:
                   {
                     $cond: { if: { $and: [ "$Running", "$hasQuery",  "$Active"] }, then: true, else: false }
                   }           
               }
          }
          ,
          {
            $match: {
                RunningActiveRecordHasQuery :  true
            }    
          },
          function (err, results) {
            if (!err ) {
              console.log (results.result);
              match={}
              if (results.length>0) {
                  match.AnyNotRunningActiveRecordHavingNoQuery=true;
              } else {
                match.AnyActiveRecordNotRunning=true;
              }
              db.test.aggregate(
              {
                   $project:
                     {
                       _id: 1,
                         Running : 1,
                         Active : 1,
                         hasQuery : 1,
                         AnyNotRunningActiveRecordHavingNoQuery:
                         {
                           $cond: { if: { $and: [ {$eq: [ "$Running", false ] }, {$eq : [ "$hasQuery", false]},  "$Active"] }, then: true, else: false }
                         },
                         AnyActiveRecordNotRunning:
                         {
                           $cond: { if: { $and: [ {$eq: [ "$Running", false ] }, "$Active"] }, then: true, else: false }
                         }             
                     }
                }
                ,
                {
                  $match: match  
                },
                function (err, docs) {
    
                }
    
              )
            }
          } 
    

    )

    它使用聚合来使事情正常工作。

    【讨论】:

      猜你喜欢
      • 2021-10-21
      • 2013-04-17
      • 1970-01-01
      • 2012-12-23
      • 2017-08-30
      • 2013-02-10
      • 2015-09-25
      • 2011-07-28
      • 2023-04-06
      相关资源
      最近更新 更多