Hello Community,
I have one query which is the combination of $and and $or.
e.g.
db.collection.find({"$and": [
{
"Obj.status": {
"$ne": "REJECTED"
}
},
{
"show": {
"$ne": false
}
},
{
"priority": {
"$ne": true
}
},
{
"$or": [
{
"pc": {
"$exists": false
}
},
{
"pc": {
"$in": [
"VALUE 1",
"VALUE 2",
"VALUE 3"
]
}
}
]
},
{
"$or": [
{
"pcv": {
"$exists": false
}
},
{
"pcv": "VALUE 4"
},
{
"pcv": "VALUE 5",
"pcvs": {
"$ne": "VALUE 6"
}
},
{
"pcv": "VALUE 7"
}
]
},
{
"$or": [
{
"uo": {
"$exists": false
}
},
{
"uo": {
"$elemMatch": {
"status": "COMPLETED"
}
}
},
{
"isLS": true,
"isLSO": true,
"isDeleted": {
"$ne": true
}
}
]
},
{
"category": {
"$exists": false
}
},
{
"type": {
"$in": [
"V",
"I"
]
}
}
]
})
Above query is just a sample, actually I have 7 $or sections inside the $and.
I saw that there is a document available for $or query, and it’s recommended to have separate index on each of the columns used in $or.
Ref: https://www.mongodb.com/docs/manual/reference/operator/query/or/#or-clauses-and-indexes
I have few questions here:
- As you can see there are multiple $or sections, so should I create a separate index for each attributes used in multiple $or?
- How should I treat the $and?
- Is the ordering of the multiple $or section matters?
- Is the ordering of the attributes inside the $or section matters?
Any help/tip to improve and index creation would be highly appreciated.
Thank you,
Faiz