Hi Everyone, I’m facing some query slowness issue in my complex set of query stages with the million dump of records.
Can any one share me the some point of thoughts, it might be helpful for me to end this process.
db.getCollection("states").aggregate([
{ "$project": { "_id": 1 } },
{
"$lookup": {
"from": "allbama",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } },
],
"as": "allbama"
}
},
{
"$lookup": {
"from": " alaska",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": " alaska"
}
},
{
"$lookup": {
"from": " arizona",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": " arizona"
}
},
{
"$lookup": {
"from": "california",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "california"
}
},
{
"$lookup": {
"from": "colorado",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "colorado"
}
},
{
"$lookup": {
"from": "florida",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "florida"
}
},
{
"$lookup": {
"from": "lowa",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "lowa"
}
},
{
"$lookup": {
"from": "maine",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "maine"
}
},
{
"$lookup": {
"from": "nevada",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "nevada"
}
},
{
"$lookup": {
"from": "oregon",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "oregon"
}
},
{
"$lookup": {
"from": "texas",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "texas"
}
},
{
"$lookup": {
"from": "utah",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "utah"
}
},
{
"$lookup": {
"from": "vermont",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "vermont"
}
},
{
"$lookup": {
"from": "virginia",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "virginia"
}
},
{
"$lookup": {
"from": "washington",
"localField": "_id",
"foreignField": "state",
"pipeline": [
{
"$match": {
"createdAt": {
"$gte": ISODate("2023-01-01T18:30:00.000Z"),
"$lte": ISODate("2024-01-31T18:30:00.000Z")
},
"flag": "Security Flag"
}
},
{ "$project": { "_id": 1 } }
],
"as": "washington"
}
},
{
"$addFields": {
"hasSecurityFlag": {
"$or": [
{ "$gt": [{ "$size": "$allbama" }, 0] },
{ "$gt": [{ "$size": "$ alaska" }, 0] },
{ "$gt": [{ "$size": "$ arizona" }, 0] },
{ "$gt": [{ "$size": "$california" }, 0] },
{ "$gt": [{ "$size": "$colorado" }, 0] },
{ "$gt": [{ "$size": "$florida" }, 0] },
{ "$gt": [{ "$size": "$lowa" }, 0] },
{ "$gt": [{ "$size": "$maine" }, 0] },
{ "$gt": [{ "$size": "$nevada" }, 0] },
{ "$gt": [{ "$size": "$oregon" }, 0] },
{ "$gt": [{ "$size": "$texas" }, 0] },
{ "$gt": [{ "$size": "$utah" }, 0] },
{ "$gt": [{ "$size": "$vermont" }, 0] },
{ "$gt": [{ "$size": "$virginia" }, 0] },
{ "$gt": [{ "$size": "$washington" }, 0] }
]
}
}
},
{
"$match": {
"hasSecurityFlag": true
}
},
]);
In this Query, I can able to get the results in below 5 seconds from lakhs of records without using the final match stage {“$match”: {“hasSecurityFlag”: true}}, But when i add this match stage it takes 3 to 4 minutes for the results.
But this is not an default collection field. So i can’t able to add appropriate index for this match stage. What can be updated to get this query process optimized and efficient. Thanks!