Is a range query possible using multikeys?

129 views
Skip to first unread message

mlanza

unread,
Mar 3, 2011, 10:15:55 PM3/3/11
to mongodb-user
Rather than reiterate my post from Stack Overflow, I'll just link it.
A member there recommended I try asking the question here:

http://stackoverflow.com/questions/5188208/mongodb-is-a-range-query-possible-using-multikeys

A couple follow up questions:

1. Clearly even if mongoDB doesn't yet support this, it could. Is
there a suggested enhancement for this (JIRA ticket)? I'd like to
vote it up.
2. Isn't it possible to have some sort of indexing option (specified
in the index command) that could allow you to index a hash's keys and
values in the manner described. It seems a better fit that an array
of hashes (as suggested in multikeys).

Thanks for reading. :)

Kyle Banker

unread,
Mar 3, 2011, 10:39:07 PM3/3/11
to mongod...@googlegroups.com
If you have this document:

var pd = {
type: "Person",
attributes: [
{name: "Penelope Doe"},
{age: 26}
]
};

And you have no idea in advance what keys and values will appear, then
you can index the attributes array:

db.docs.ensureIndex({attributes: 1})

Then you can query on any attribute, but notice that you match against
the entire document:

db.docs.find({attributes: {age: 26})

You can also do a range scan like this, but you need to be careful to
specify both bounds (ot):

db.docs.find({attributes: {$gt: {age: 26}, $lt: {age: 100});

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Lucas Zamboulis

unread,
Mar 4, 2011, 4:51:33 AM3/4/11
to mongod...@googlegroups.com, Kyle Banker
A couple of questions on this:

a) Why does one need to specify both bounds? Does this mean that if
only one bound is specified then the index will not be used?

b) I seem to remember that for compound indexes, the field taking part
in a range query has to be last in index definition. Otherwise the
index may not be used and if it is used then the query will not be as
fast as if it was specified last in the index definition. Am I right
about this (docs say order is important but not in what way - can you
please clarify them)?

c) If I'm right about (b), does anything similar apply in this case,
where the multikey index is on an array with (sub)documents as items?
And if so, is there a way to know the order that the fields are
defined in the multikey index?

Lucas

Kyle Banker

unread,
Mar 4, 2011, 8:10:23 AM3/4/11
to mongod...@googlegroups.com
Answers below:

On Fri, Mar 4, 2011 at 4:51 AM, Lucas Zamboulis <lzamb...@gmail.com> wrote:
> A couple of questions on this:
> a) Why does one need to specify both bounds? Does this mean that if
> only one bound is specified then the index will not be used?

Because here you're matching against an entire object, not a field.
You're trying to get everything greater than the object {age: 100}. So
{name: 100} would be included in that range. This is a case of
indexing an entire sub-document, not a field within a sub-document.

> b) I seem to remember that for compound indexes, the field taking part
> in a range query has to be last in index definition. Otherwise the
> index may not be used and if it is used then the query will not be as
> fast as if it was specified last in the index definition. Am I right
> about this (docs say order is important but not in what way - can you
> please clarify them)?

This isn't a compound index; it's a multi-key index (which just means
that you're indexing each element in an array.).

> c) If I'm right about (b), does anything similar apply in this case,
> where the multikey index is on an array with (sub)documents as items?
> And if so, is there a way to know the order that the fields are
> defined in the multikey index?

See answers to a) and b).

Reply all
Reply to author
Forward
0 new messages