Ok, two part answer.
1. For $exists: true the index doesn't reduce the number of scanned objects because the index entry for an explicit null and $exists: false look the same so it has to scan the object to determine if it is an explicit null or just doesn't exist. That lack of additinal selectivity from the compound index is likely why it wasn't selected without the hint even though intuitively it seemed like it would help. The compound index should help with $ne: null but the semantic is slightly different - would that work for your app?
2. As to why more records are returned with the hinted index, I suspect a bug. What version of mongodb are you running?
-- Max
Looking at another issue you were discussing with Scott it appears you are running 1.8. If this issue occured on 1.8 as well, then you hit some issues around $exists and indexes and ors. Your query might work without the $or (assuming the $exists is the only other clause) but I'd recommend an upgrade to 2.0 anyhow if possible.
If you're curious this looks like the fix:
https://github.com/mongodb/mongo/commit/ f97863227992f5e25ebe0882875d54cc5cc5937a
Hope this helps and sorry for the false start I missed the middle query with the hint entirely which was the really interesting one!
-- Max
Looking at another issue you were discussing with Scott it appears you are running 1.8. If this issue occured on 1.8 as well, then you hit some issues around $exists and indexes and ors. Your query might work without the $or (assuming the $exists is the only other clause) but I'd recommend an upgrade to 2.0 anyhow if possible.
If you're curious this looks like the fix:
https://github.com/mongodb/mongo/commit/ f97863227992f5e25ebe0882875d54cc5cc5937a
Hope this helps and sorry for the false start I missed the middle query with the hint entirely which was the really interesting one!
-- Max
On May 14, 2012 11:55 PM, "Max Schireson" <> wrote:
Ok, two part answer.
1. For $exists: true the index doesn't reduce the number of scanned objects because the index entry for an explicit null and $exists: false look the same so it has to scan the object to determine if it is an explicit null or just doesn't exist. That lack of additinal selectivity from the compound index is likely why it wasn't selected without the hint even though intuitively it seemed like it would help. The compound index should help with $ne: null but the semantic is slightly different - would that work for your app?
2. As to why more records are returned with the hinted index, I suspect a bug. What version of mongodb are you running?
-- Max
On May 14, 2012 9:53 PM, "Max Schireson" <> wrote:
Sorry my bad you tried that already and hinted it in the second
example and it did not behave as expected.
I am not sure what is up I will take a look.
-- Max