Simplified example and question
In effort to be as simple as possible, I'll just jump into an example:
Collection foo
with multikey index {searchTags: 1}
:
{_id: 1, searchTags: [{bar: "BAR"}]}
{_id: 2, searchTags: [{baz: "BAZ"}]}
I'm trying to get all embedded documents with the baz
field key (without using $exists
- I can try to explain why later). Why does
{searchTags: {$elemMatch: { $gte: { baz: MinKey() }, $lte: { baz: MaxKey() }}}}
return BOTH documents (not preferred), but
{searchTags: {$elemMatch: { $gte: { baz: "" }, $lte: { baz: MaxKey() }}}}
only returns {_id: 2, searchTags: [{baz: "BAZ"}]}
(preferred)?
Sidenote: quick schema details to maybe avoid the "what are you doing?" questions
- These are simple embedded documents that only have 1 key-value pair.
- The values can be multiple types, not just strings (otherwise, I would have just used an array of prefixed strings instead of embedded documents)
- Being that the embedded docs are singular key-value pairs, I can deterministically use equality and comparison queries on the embedded docs.
- The keys are other field names on
foo
documents, and the values are their field values. I could index each of the various fields, but they would be partial indexes and there would be a lot of them. For flexibility, merging them all into an array with a multikey index makes the most sense. - Still open to hear why I'm wrong :)