This is the part of the model schema that is being used in the query
{
falAuthorizationStatus: {
type: String,
},
isVerifiedByNafath: {
type: Boolean,
default: false,
},
status: {
type: String,
},
role: {
type: String,
default: 'GUEST',
trim: true,
},
subscription: {
type: new Schema({
packageName: { type: String },
}, { _id: false }),
},
districtsOfInterest: {
type: [new Schema({
_id: {
type: Schema.Types.ObjectId,
ref: 'District',
},
}, { id: false })],
default: [],
},
userActivites: {
requestsCommuincationsCounts: { type: Number, default: 0 },
},
}
and this is the index used for this query
schema.index({
'districtsOfInterest._id': 1,
isVerifiedByNafath: -1,
falAuthorizationStatus: 1,
'subscription.packageName': -1,
'userActivites.requestsCommuincationsCounts': -1,
}, {
partialFilterExpression: {
role: 'REAL_STATE_AGENT',
status: 'ACTIVE',
'subscription.packageName': {
$in: [
'PROFESSIONAL_ONE_YEAR',
'PROFESSIONAL_SIX_MONTH',
'PROFESSIONAL_THREE_MONTH',
'PROFESSIONAL_ONE_MONTH',
'FREE_PROFESSIONAL',
],
},
},
name: 'active-professional-agents',
});
and this is the query with sort
[
{
$match: {
_id: {
$ne: ObjectId("6516d342b58ff3ad152babf8"),
},
status: "ACTIVE",
role: "REAL_STATE_AGENT",
"subscription.packageName": {
$in: [
"PROFESSIONAL_ONE_YEAR",
"PROFESSIONAL_SIX_MONTH",
"PROFESSIONAL_THREE_MONTH",
"PROFESSIONAL_ONE_MONTH",
"FREE_PROFESSIONAL",
],
},
"districtsOfInterest._id": {
$in: [
ObjectId("5dd490aebc2e91004242e469"),
ObjectId("5dd490aebc2e91004242e48e"),
ObjectId("5dd490aebc2e91004242e49a"),
],
},
},
},
{
$sort: {
isVerifiedByNafath: -1,
falAuthorizationStatus: 1,
"subscription.packageName": -1,
"userActivites.requestsCommuincationsCounts":
-1,
},
},
]
Also here is a json file for execution stats from compass
exeuction-stats-users.json (211.4 KB)