Index to cover explicit $and and $or

I have a sample collection foo

collection foo
{
"name:“Ace”
"city:“Dallas”
“state”:“tx”
“zipcode”:78751
“age”:“30”
}

If I run this query what type of index should be used?

db.foo.find({$and: [ { name: “Ace” }, { $or: [ { “zipcode”: “78751” }, { “age”: “30” } ] } ] })

would the following work compound index work
db.createIndex({name:1,zipcode:1})
db.createIndex({name:1,age:1})

or would I need an individual index for:
db.createIndex({name:1})
db.createIndex({zipcode:1})
db.createIndex({age:1})

@ maythee

This is ahead of the course materials at this point. The topics you raise will be covered in Chapters 3 & 4; I’d suggest that you wait on this question until you have finished those sections and then refine your question in light of the materials there. Good luck.

1 Like