Yes, a compound index can be used for both queries. However, it depends on how you establish your compound index; realize that the field order in the index is important.
If you want to do both of these queries:
db.user.find({'email': <some_email>, 'zipcode': <some_zipcode>})
db.user.find({'email': <some_email>)
Then it is important that when you establish your index that you create your compound index with the email
field first.
For example:
db.users.createIndex({
"email" : 1,
"stock" : 1
})
This will allow db.user.find({'email': <some_email>)
to take advantage of the compound index because the email
field is an index prefix; in other words, the field is at the beginning of the indexed fields.
Read about Compound Index Prefixes to gain a better understanding of this concept.
Also, see MongoDB's Compound Indexes for more information on compound indexes in general.