What about $expr in $match
My Model example
{
"_id" : ObjectId("622f573cb9fd75a8f988cdb6"),
"branchId" : ObjectId("6212f2fa0615b313e2eb83f5"),
"groupId" : ObjectId("622f573cb9fd75a8f988cdb4"),
"teacherId" : ObjectId("622f4f70475460a853fd8fa1"),
"date" : ISODate("2022-03-15T00:00:00.000+0000"),
"state" : "created",
"createdAt" : ISODate("2022-03-14T14:54:52.850+0000"),
"updatedAt" : ISODate("2022-03-14T14:54:52.850+0000"),
"deletedAt" : ISODate("2022-03-16T09:04:15.740+0000")
}
Indexes of my collection
[
{
"v" : 2.0,
"key" : {
"_id" : 1.0
},
"name" : "_id_"
},
{
"v" : 2.0,
"key" : {
"date" : 1.0,
"groupId" : 1.0
},
"name" : "UniqueGruopLesson",
"background" : true,
"unique" : true,
"partialFilterExpression" : {
"deletedAt" : {
"$eq" : null
}
}
}
]
Situation 1.
db = db.getSiblingDB("TEST_DB");
db.getCollection("LESSONS").explain("executionStats")
.aggregate(
[
{
$match: {
groupId: ObjectId("627f7821c5e9de1b328ea918"),
date: ISODate("2022-05-30T00:00:00.000+0000"),
deletedAt:null
}
}
]
);
Result 1
{
"explainVersion" : "1",
"queryPlanner" : {
"namespace" : "TEST_DB.LESSONS",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"date" : {
"$eq" : ISODate("2022-05-30T00:00:00.000+0000")
}
},
{
"deletedAt" : {
"$eq" : null
}
},
{
"groupId" : {
"$eq" : ObjectId("627f7821c5e9de1b328ea918")
}
}
]
},
"optimizedPipeline" : true,
"maxIndexedOrSolutionsReached" : false,
"maxIndexedAndSolutionsReached" : false,
"maxScansToExplodeReached" : false,
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"deletedAt" : {
"$eq" : null
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"date" : 1.0,
"groupId" : 1.0
},
"indexName" : "UniqueGruopLesson",
"isMultiKey" : false,
"multiKeyPaths" : {
"date" : [
],
"groupId" : [
]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"date" : [
"[new Date(1653868800000), new Date(1653868800000)]"
],
"groupId" : [
"[ObjectId('627f7821c5e9de1b328ea918'), ObjectId('627f7821c5e9de1b328ea918')]"
]
}
}
},
"rejectedPlans" : [
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1.0,
"executionTimeMillis" : 0.0,
"totalKeysExamined" : 1.0,
"totalDocsExamined" : 1.0,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"deletedAt" : {
"$eq" : null
}
},
"nReturned" : 1.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 2.0,
"advanced" : 1.0,
"needTime" : 0.0,
"needYield" : 0.0,
"saveState" : 0.0,
"restoreState" : 0.0,
"isEOF" : 1.0,
"docsExamined" : 1.0,
"alreadyHasObj" : 0.0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 2.0,
"advanced" : 1.0,
"needTime" : 0.0,
"needYield" : 0.0,
"saveState" : 0.0,
"restoreState" : 0.0,
"isEOF" : 1.0,
"keyPattern" : {
"date" : 1.0,
"groupId" : 1.0
},
"indexName" : "UniqueGruopLesson",
"isMultiKey" : false,
"multiKeyPaths" : {
"date" : [
],
"groupId" : [
]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"date" : [
"[new Date(1653868800000), new Date(1653868800000)]"
],
"groupId" : [
"[ObjectId('627f7821c5e9de1b328ea918'), ObjectId('627f7821c5e9de1b328ea918')]"
]
},
"keysExamined" : 1.0,
"seeks" : 1.0,
"dupsTested" : 0.0,
"dupsDropped" : 0.0
}
}
},
"command" : {
"aggregate" : "LESSONS",
"pipeline" : [
{
"$match" : {
"groupId" : ObjectId("627f7821c5e9de1b328ea918"),
"date" : ISODate("2022-05-30T00:00:00.000+0000"),
"deletedAt" : null
}
}
],
"cursor" : {
},
"$db" : "TEST_DB"
},
"serverInfo" : {
"host" : "nitro",
"port" : 2717.0,
"version" : "5.0.8",
"gitVersion" : "c87e1c23421bf79614baf500fda6622bd90f674e"
},
"serverParameters" : {
"internalQueryFacetBufferSizeBytes" : 104857600.0,
"internalQueryFacetMaxOutputDocSizeBytes" : 104857600.0,
"internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600.0,
"internalDocumentSourceGroupMaxMemoryBytes" : 104857600.0,
"internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600.0,
"internalQueryProhibitBlockingMergeOnMongoS" : 0.0,
"internalQueryMaxAddToSetBytes" : 104857600.0,
"internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600.0
},
"ok" : 1.0,
"$clusterTime" : {
"clusterTime" : Timestamp(1652524686, 1),
"signature" : {
"hash" : BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1652524686, 1)
}
Situation 1 working fine but
Situation 2.
with $expr
scanning all documents in collection
why I need it to be $expr
because then I need it to be inside $lookup
db = db.getSiblingDB("TEST_DB");
db.getCollection("LESSONS").explain("executionStats")
.aggregate(
[
{
$match: {
$expr: {
$and: [
{
$eq: [
"$groupId",
ObjectId("627f7821c5e9de1b328ea918"),
]
},
{
"$eq": [
"$deletedAt",
null
]
},
{
$eq:[
"$date",
ISODate("2022-05-30T00:00:00.000+0000")
]
}
]
}
}
}
]
);
Result 2
{
"explainVersion" : "1",
"queryPlanner" : {
"namespace" : "TEST_DB.LESSONS",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"$expr" : {
"$and" : [
{
"$eq" : [
"$groupId",
{
"$const" : ObjectId("627f7821c5e9de1b328ea918")
}
]
},
{
"$eq" : [
"$deletedAt",
{
"$const" : null
}
]
},
{
"$eq" : [
"$date",
{
"$const" : ISODate("2022-05-30T00:00:00.000+0000")
}
]
}
]
}
},
{
"date" : {
"$_internalExprEq" : ISODate("2022-05-30T00:00:00.000+0000")
}
},
{
"deletedAt" : {
"$_internalExprEq" : null
}
},
{
"groupId" : {
"$_internalExprEq" : ObjectId("627f7821c5e9de1b328ea918")
}
}
]
},
"optimizedPipeline" : true,
"maxIndexedOrSolutionsReached" : false,
"maxIndexedAndSolutionsReached" : false,
"maxScansToExplodeReached" : false,
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [
{
"$expr" : {
"$and" : [
{
"$eq" : [
"$groupId",
{
"$const" : ObjectId("627f7821c5e9de1b328ea918")
}
]
},
{
"$eq" : [
"$deletedAt",
{
"$const" : null
}
]
},
{
"$eq" : [
"$date",
{
"$const" : ISODate("2022-05-30T00:00:00.000+0000")
}
]
}
]
}
},
{
"date" : {
"$_internalExprEq" : ISODate("2022-05-30T00:00:00.000+0000")
}
},
{
"deletedAt" : {
"$_internalExprEq" : null
}
},
{
"groupId" : {
"$_internalExprEq" : ObjectId("627f7821c5e9de1b328ea918")
}
}
]
},
"direction" : "forward"
},
"rejectedPlans" : [
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1.0,
"executionTimeMillis" : 25.0,
"totalKeysExamined" : 0.0,
"totalDocsExamined" : 28851.0,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [
{
"$expr" : {
"$and" : [
{
"$eq" : [
"$groupId",
{
"$const" : ObjectId("627f7821c5e9de1b328ea918")
}
]
},
{
"$eq" : [
"$deletedAt",
{
"$const" : null
}
]
},
{
"$eq" : [
"$date",
{
"$const" : ISODate("2022-05-30T00:00:00.000+0000")
}
]
}
]
}
},
{
"date" : {
"$_internalExprEq" : ISODate("2022-05-30T00:00:00.000+0000")
}
},
{
"deletedAt" : {
"$_internalExprEq" : null
}
},
{
"groupId" : {
"$_internalExprEq" : ObjectId("627f7821c5e9de1b328ea918")
}
}
]
},
"nReturned" : 1.0,
"executionTimeMillisEstimate" : 3.0,
"works" : 28853.0,
"advanced" : 1.0,
"needTime" : 28851.0,
"needYield" : 0.0,
"saveState" : 28.0,
"restoreState" : 28.0,
"isEOF" : 1.0,
"direction" : "forward",
"docsExamined" : 28851.0
}
},
"command" : {
"aggregate" : "LESSONS",
"pipeline" : [
{
"$match" : {
"$expr" : {
"$and" : [
{
"$eq" : [
"$groupId",
ObjectId("627f7821c5e9de1b328ea918")
]
},
{
"$eq" : [
"$deletedAt",
null
]
},
{
"$eq" : [
"$date",
ISODate("2022-05-30T00:00:00.000+0000")
]
}
]
}
}
}
],
"cursor" : {
},
"$db" : "TEST_DB"
},
"serverInfo" : {
"host" : "nitro",
"port" : 2717.0,
"version" : "5.0.8",
"gitVersion" : "c87e1c23421bf79614baf500fda6622bd90f674e"
},
"serverParameters" : {
"internalQueryFacetBufferSizeBytes" : 104857600.0,
"internalQueryFacetMaxOutputDocSizeBytes" : 104857600.0,
"internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600.0,
"internalDocumentSourceGroupMaxMemoryBytes" : 104857600.0,
"internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600.0,
"internalQueryProhibitBlockingMergeOnMongoS" : 0.0,
"internalQueryMaxAddToSetBytes" : 104857600.0,
"internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600.0
},
"ok" : 1.0,
"$clusterTime" : {
"clusterTime" : Timestamp(1652524646, 1),
"signature" : {
"hash" : BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1652524646, 1)
}