MongoDB Aggregation SQL Union and SQL Exists like clause -


i make mongodb aggregation query on data :

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 represented table architechture 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 apply following query on table :

select * exists(select * a.running=true , a.hasquery=true) , a.running=false , a.hasquery=false , a.active=true  union  select * not exists(select * a.running=true , a.hasquery=true) , a.running=false , a.active=true 

i 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 same thing mongodb aggregation ?

i managed 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 aggregation make things working.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -