Mongo 聚合查询 - 合并两个 AND 查询
Mongo Aggregate query - combine two AND queries
我的数据库中有以下合集
{
"_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"
}
我想要的输出是:
所有文档,其中 'firstid' 和 'secondid' 字段是文档 ID 之一且 'year' 字段大于或等于 2018
AND 'firstid' + 'secondid' 同时出现在文档中 AND '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 查询从第二个条件检索文档
$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"
}
为什么不结合两个条件?
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"
}
我的数据库中有以下合集
{
"_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"
}
我想要的输出是:
所有文档,其中 'firstid' 和 'secondid' 字段是文档 ID 之一且 'year' 字段大于或等于 2018
AND 'firstid' + 'secondid' 同时出现在文档中 AND '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 查询从第二个条件检索文档
$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"
}
为什么不结合两个条件?
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"
}