简体   繁体   English

MongoDB聚合SQL Union和SQL Exists子句

[英]MongoDB Aggregation SQL Union and SQL Exists like clause

I would like to make a MongoDB aggregation Query on data like this : 我想对像这样的数据进行MongoDB聚合查询:

Collection A 集合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 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

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 : 我得到这些结果:在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 : 在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 ? mongoDB聚合如何做同样的事情?

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. 它用于聚合以使事情正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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