Mongo汇总查询-结合两个AND查询

时间:2018-12-05 06:59:28

标签: mongodb aggregation-framework querying

我的数据库中有以下收藏集

{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "2Z00CInO",
    "firstid" : "markt1995",
    "secondid" : "ninee87"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "fl981nhg",
    "firstid" : "violentee9",
    "secondid" : "markt1995"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "birk8mn",
    "firstid" : "eve1992",
    "secondid" : "toms78"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2015",
    "identifier" : "09man1l",
    "firstid" : "markt1995",
    "secondid" : "eve1992"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2017",
    "identifier" : "8h06KnO",
    "firstid" : "markt1995",
    "secondid" : "zepp988"
}

我想要的输出是:

  1. “ firstid”和“ secondid”字段是文档ID且“ year”字段大于或等于2018的所有文档

  2. AND'firstid'+'secondid'一次出现在文档中,并且'year'字段大于或等于2015

我有$ match阶段,它们分别选择了1.和2.,但是我无法将它们组合在一起以得到一个输出。

我有查询变量的值,我正在使用nodejs:

var firstid = markt1995

var secondid = eve1992

有$ match查询可从第一个critera中检索文档

$match: {
            $and: [{
              $or: [{
                  'firstid': {
                    "$in": [firstid, secondid]
                  }
                },
                {
                  'secondid': {
                    "$in": [firstid, secondid]
                  }
                }
              ],
              $and: [{
                'year': {
                  $gte: 2018
                }
              }],
            }]
    }

输出:

{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "2Z00CInO",
    "firstid" : "markt1995",
    "secondid" : "ninee87"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "fl981nhg",
    "firstid" : "violentee9",
    "secondid" : "markt1995"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "birk8mn",
    "firstid" : "eve1992",
    "secondid" : "toms78"
}

有$ match查询可从第二个critera中检索文档

$and: [{
              $or: [{
                'firstid': {
                  "$in": [firstid, secondid]
                },
                'secondid': {
                  "$in": [firstid, secondid]
                }
              }],
              $and: [{
                'year': {
                  $gte: 2015
                }
              }]

          }]

输出:

{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2015",
    "identifier" : "09man1l",
    "firstid" : "markt1995",
    "secondid" : "eve1992"
}

在一个查询中所需的输出:

{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "2Z00CInO",
    "firstid" : "markt1995",
    "secondid" : "ninee87"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "fl981nhg",
    "firstid" : "violentee9",
    "secondid" : "markt1995"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2018",
    "identifier" : "birk8mn",
    "firstid" : "eve1992",
    "secondid" : "toms78"
},
{
    "_id" : ObjectId("5bdba3101efdd9172de0b52f"),
    "year" : "2015",
    "identifier" : "09man1l",
    "firstid" : "markt1995",
    "secondid" : "eve1992"
}

1 个答案:

答案 0 :(得分:1)

为什么不合并两个条件?

var firstid = 'markt1995';
var secondid = 'eve1992';
var a = [firstid,secondid ]
var c1 = {year:{$gte:2018},$or:[{firstid:{$in:a}},{secondid:{$in:a}}]};
var c2 = {year:{$gte:2015},firstid:{$in:a},secondid:{$in:a}};
db.getCollection('stack1').find(
    {$or:[c1,c2]}
)

答案

/* 1 */
{
    "_id" : ObjectId("5c078dd0410f22947d29dddc"),
    "year" : 2018,
    "identifier" : "2Z00CInO",
    "firstid" : "markt1995",
    "secondid" : "ninee87"
}

/* 2 */
{
    "_id" : ObjectId("5c078dd0410f22947d29dddd"),
    "year" : 2018.0,
    "identifier" : "fl981nhg",
    "firstid" : "violentee9",
    "secondid" : "markt1995"
}

/* 3 */
{
    "_id" : ObjectId("5c078dd0410f22947d29ddde"),
    "year" : 2018.0,
    "identifier" : "birk8mn",
    "firstid" : "eve1992",
    "secondid" : "toms78"
}

/* 4 */
{
    "_id" : ObjectId("5c078dd0410f22947d29dddf"),
    "year" : 2015,
    "identifier" : "09man1l",
    "firstid" : "markt1995",
    "secondid" : "eve1992"
}