Thank you @steevej for your time. The exact pipeline is:
[
{
$match: {
"between.objectId":
"ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg"
}
},
{
$addFields: {
between: {
$first: {
$filter: {
input: "$between",
cond: {
$ne: [
"$$this.objectId",
"ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg"
]
}
}
}
}
}
},
{
$lookup: {
from: "Profile",
localField: "between.objectId",
foreignField: "_id",
as: "profile",
pipeline: [
{
$project: {
_id: 1,
syncedAt: 1
}
}
]
}
}
]
and the explain:
{
"explainVersion": "1",
"stages": [
{
"$cursor": {
"queryPlanner": {
"namespace": "prod.Connection",
"indexFilterSet": false,
"parsedQuery": {
"between.objectId": {
"$eq": "ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg"
}
},
"queryHash": "E13BCAF8",
"planCacheKey": "AA28C8B2",
"maxIndexedOrSolutionsReached": false,
"maxIndexedAndSolutionsReached": false,
"maxScansToExplodeReached": false,
"winningPlan": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {
"between.objectId": 1,
"_p_disconnectOp": 1,
"disconnectedAt": 1
},
"indexName": "between.objectId_1__p_disconnectOp_1_disconnectedAt_1",
"isMultiKey": true,
"multiKeyPaths": {
"between.objectId": ["between"],
"_p_disconnectOp": [],
"disconnectedAt": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"between.objectId": [
"[\"ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg\", \"ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg\"]"
],
"_p_disconnectOp": [
"[MinKey, MaxKey]"
],
"disconnectedAt": [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans": []
},
"executionStats": {
"executionSuccess": true,
"nReturned": 23138,
"executionTimeMillis": 9333,
"totalKeysExamined": 23138,
"totalDocsExamined": 23138,
"executionStages": {
"stage": "FETCH",
"nReturned": 23138,
"executionTimeMillisEstimate": 43,
"works": 23139,
"advanced": 23138,
"needTime": 0,
"needYield": 0,
"saveState": 27,
"restoreState": 27,
"isEOF": 1,
"docsExamined": 23138,
"alreadyHasObj": 0,
"inputStage": {
"stage": "IXSCAN",
"nReturned": 23138,
"executionTimeMillisEstimate": 4,
"works": 23139,
"advanced": 23138,
"needTime": 0,
"needYield": 0,
"saveState": 27,
"restoreState": 27,
"isEOF": 1,
"keyPattern": {
"between.objectId": 1,
"_p_disconnectOp": 1,
"disconnectedAt": 1
},
"indexName": "between.objectId_1__p_disconnectOp_1_disconnectedAt_1",
"isMultiKey": true,
"multiKeyPaths": {
"between.objectId": ["between"],
"_p_disconnectOp": [],
"disconnectedAt": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"between.objectId": [
"[\"ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg\", \"ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg\"]"
],
"_p_disconnectOp": [
"[MinKey, MaxKey]"
],
"disconnectedAt": [
"[MinKey, MaxKey]"
]
},
"keysExamined": 23138,
"seeks": 1,
"dupsTested": 23138,
"dupsDropped": 0
}
}
}
},
"nReturned": 23138,
"executionTimeMillisEstimate": 126
},
{
"$addFields": {
"between": {
"$first": [
{
"$filter": {
"input": "$between",
"as": "this",
"cond": {
"$ne": [
"$$this.objectId",
{
"$const": "ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg"
}
]
}
}
}
]
}
},
"nReturned": 23138,
"executionTimeMillisEstimate": 152
},
{
"$lookup": {
"from": "Profile",
"as": "profile",
"localField": "between.objectId",
"foreignField": "_id",
"let": {},
"pipeline": [
{
"$project": {
"_id": 1,
"syncedAt": 1
}
}
]
},
"totalDocsExamined": 23138,
"totalKeysExamined": 23138,
"collectionScans": 0,
"indexesUsed": ["_id_"],
"nReturned": 23138,
"executionTimeMillisEstimate": 9323
}
],
"serverInfo": {
"host": "atlas-zl72i1-shard-00-02.avkshgd.mongodb.net",
"port": 27017,
"version": "6.0.19",
"gitVersion": "a7ada5ff3a4d8a1e2ed88f86bd6b3d1d16cb43c6"
},
"serverParameters": {
"internalQueryFacetBufferSizeBytes": 104857600,
"internalQueryFacetMaxOutputDocSizeBytes": 104857600,
"internalLookupStageIntermediateDocumentMaxSizeBytes": 104857600,
"internalDocumentSourceGroupMaxMemoryBytes": 104857600,
"internalQueryMaxBlockingSortMemoryUsageBytes": 104857600,
"internalQueryProhibitBlockingMergeOnMongoS": 0,
"internalQueryMaxAddToSetBytes": 104857600,
"internalDocumentSourceSetWindowFieldsMaxMemoryBytes": 104857600
},
"command": {
"aggregate": "Connection",
"pipeline": [
{
"$match": {
"between.objectId": "ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg"
}
},
{
"$addFields": {
"between": {
"$first": {
"$filter": {
"input": "$between",
"cond": {
"$ne": [
"$$this.objectId",
"ACoAAACQPmoB2OL6tWoksPNyCwpBpSeg"
]
}
}
}
}
}
},
{
"$lookup": {
"from": "Profile",
"localField": "between.objectId",
"foreignField": "_id",
"as": "profile",
"pipeline": [
{
"$project": {
"_id": 1,
"syncedAt": 1
}
}
]
}
}
],
"cursor": {},
"maxTimeMS": 60000,
"$db": "prod"
},
"ok": 1,
"$clusterTime": {
"clusterTime": {
"$timestamp": "7436555763570442241"
},
"signature": {
"hash": "bZn4WGbLoOKv1bqgTGIs6//E/4s=",
"keyId": {
"low": 8,
"high": 1722412984,
"unsigned": false
}
}
},
"operationTime": {
"$timestamp": "7436555763570442241"
}
}