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.
>
>
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
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).