MongoDB Aggregation SQL Union and SQL Exists like clause

你说的曾经没有我的故事 提交于 2019-12-13 06:49:47

问题


I would like to make a MongoDB aggregation Query on data like this :

Collection 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,
}

This JSON data could be represented by a table architechture like this Table 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

If I apply the following query on the table :

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

I get theses results : In 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,
}

in SQL :

 PrimaryKey   |    Active    |    hasQuery    |       Running

  1           |     true     |    false       |        false

  2           |     true     |    true        |        false

  5           |     false    |     false      |        false

How to do the same thing with mongoDB aggregation ?


回答1:


I managed to do it with that code :

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) {

            }

          )
        }
      } 

)

It uses to aggregation to make things working.



来源:https://stackoverflow.com/questions/30606996/mongodb-aggregation-sql-union-and-sql-exists-like-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!