Hi again,
On point 1 : I timed it both with Golang performance tests and also how much time took the mongodb to respond on the script. We are seeing this results both on local setups where client and server and running on the same machine and also on deployed ones.
On point 2: Ive run it a lot of times and timings are the same.
Here is the explain:
{
"stages" : [
{
"$cursor" : {
"query" : {
"$and" : [
{
"$and" : [
{
"accountId" : {
"$eq" : ObjectId("634d6e4e8b661fe80d614513")
}
},
{
"$nor" : [
{
"isDeleted" : {
"$eq" : true
}
}
]
}
]
},
{
"$nor" : [
{
"isSystem" : {
"$eq" : true
}
}
]
}
]
},
"fields" : {
"accountId" : 1,
"auth0Cache.usermetadata.firstName" : 1,
"auth0Cache.usermetadata.lastName" : 1,
"dynamicMetricCount" : 1,
"manualMetricCount" : 1,
"metrics" : 1,
"_id" : 1
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test_employees_v2.users",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"accountId" : {
"$eq" : ObjectId("634d6e4e8b661fe80d614513")
}
},
{
"$nor" : [
{
"isDeleted" : {
"$eq" : true
}
}
]
},
{
"$nor" : [
{
"isSystem" : {
"$eq" : true
}
}
]
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"$nor" : [
{
"isDeleted" : {
"$eq" : true
}
}
]
},
{
"$nor" : [
{
"isSystem" : {
"$eq" : true
}
}
]
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"accountId" : 1,
"subscriptionType" : 1
},
"indexName" : "accountId_1_subscriptionType_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"accountId" : [ ],
"subscriptionType" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"accountId" : [ "[ObjectId('634d6e4e8b661fe80d614513'), ObjectId('634d6e4e8b661fe80d614513')]" ],
"subscriptionType" : [ "[MinKey, MaxKey]" ]
}
}
},
"rejectedPlans" : [ ]
}
}
},
{
"$lookup" : {
"from" : "metrics",
"as" : "metrics",
"let" : {
"userId" : "$_id"
},
"pipeline" : [
{
"$project" : {
"fieldName" : 1,
"accountId" : 1,
"isDeleted" : 1,
"ownerIds" : 1,
"auth0Cache" : 1,
"goalId" : 1
}
},
{
"$match" : {
"$and" : [
{
"accountId" : ObjectId("634d6e4e8b661fe80d614513")
},
{
"isSystem" : {
"$ne" : true
}
}
]
}
},
{
"$match" : {
"$expr" : {
"$in" : [ "$$userId", "$ownerIds" ]
}
}
}
]
}
},
{
"$addFields" : {
"manualMetricCount" : {
"$size" : [
{
"$filter" : {
"input" : "$metrics",
"as" : "metrics",
"cond" : {
"$eq" : [
"$$metrics.fieldName",
{
"$const" : ""
}
]
}
}
}
]
},
"dynamicMetricCount" : {
"$size" : [
{
"$filter" : {
"input" : "$metrics",
"as" : "metrics",
"cond" : {
"$ne" : [
"$$metrics.fieldName",
{
"$const" : ""
}
]
}
}
}
]
}
}
},
{
"$match" : {
"dynamicMetricCount" : {
"$gt" : 1
}
}
},
{
"$facet" : {
"items" : [
{
"$sort" : {
"sortKey" : {
"auth0Cache.usermetadata.firstName" : 1,
"_id" : 1
},
"limit" : 100
}
},
{
"$project" : {
"_id" : true,
"dynamicMetricCount" : true,
"accountId" : true,
"manualMetricCount" : true,
"auth0Cache" : {
"usermetadata" : {
"lastName" : true
}
}
}
}
],
"totalCount" : [
{
"$group" : {
"_id" : {
"$const" : null
},
"totalCount" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$project" : {
"_id" : false,
"totalCount" : true
}
}
]
}
},
{
"$project" : {
"_id" : true,
"items" : true,
"totalCount" : {
"$arrayElemAt" : [
"$totalCount",
{
"$const" : 0
}
]
}
}
},
{
"$project" : {
"_id" : true,
"items" : true,
"totalCount" : "$totalCount.totalCount"
}
}
],
"ok" : 1,
"operationTime" : Timestamp(1666600486, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1666600486, 1),
"signature" : {
"hash" : BinData(0,"RTt0l2k9ECaNmPQz+Tt9eQHxIEA="),
"keyId" : NumberLong("7132835261448192001")
}
}
}
For the lookup it seems to be no data for the explain, maybe i should provide some parameter to the explain. Here the project is the first thing in the lookup pipeline. Before adding it there there are no project and this script ran 2-3 times slower than now.