简体   繁体   中英

MongoDB Aggregation SQL Union and SQL Exists like clause

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM