Dot-Notation and index usage

475 views
Skip to first unread message

gonzoprosperity

unread,
Apr 14, 2011, 6:42:22 PM4/14/11
to mongodb-user
MongoDB 1.8.1

My documents look like:

users : {
first_name: "Foo",
ext_id: {
id: "6708526",
type: 2
}
}

I have a dual index on "ext_id.id" and "ext_id.type".

db.users.find({'ext_id.id':"6708526", 'ext_id.type':1}).explain()

uses the index.

However,

db.users.find({'ext_id':{'id':"6708526", 'type':1}}).explain()

Does not. Both return my expected document, though.

From my understanding of the docs at:
http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

I *should* be using the sub-object notation (#2). But I definitely
wont be using the query that doesnt the index.

I am understanding the docs correctly? Why isnt the query with sub-
object not using the index?

Thanks!

Alvin Richards

unread,
Apr 14, 2011, 7:01:21 PM4/14/11
to mongodb-user
Looks like a problem with matching the indexed field names, so have
opened

https://jira.mongodb.org/browse/SERVER-2953

Clearly the workaround is to use the dot notation for now.

-Alvin

On Apr 14, 3:42 pm, gonzoprosperity <tool...@gmail.com> wrote:
> MongoDB 1.8.1
>
> My documents look like:
>
> users : {
>    first_name: "Foo",
>    ext_id: {
>        id: "6708526",
>        type: 2
>    }
>
> }
>
> I have a dual index on "ext_id.id"  and "ext_id.type".
>
> db.users.find({'ext_id.id':"6708526", 'ext_id.type':1}).explain()
>
> uses the index.
>
> However,
>
> db.users.find({'ext_id':{'id':"6708526", 'type':1}}).explain()
>
> Does not. Both return my expected document, though.
>
> From my understanding of the docs at:http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Obj...

Gaetan Voyer-Perrault

unread,
Apr 14, 2011, 7:39:04 PM4/14/11
to mongod...@googlegroups.com
So it's not obvious right off the top, but these are actually two different queries:

(1) db.users.find({'ext_id.id':"6708526", 'ext_id.type':1})
(2) db.users.find({'ext_id':{'id':"6708526", 'type':1}})

Query #1 would match the following document; #2 would not. (note the extra field)
users : {
  first_name: "Foo",
  ext_id: {
      id: "6708526",
      type: 2,
      name : 'test'
  }
}

===
The following build two different indexes
(a) db.users.ensureIndex({'ext_id':1})
(b) db.users.ensureIndex({'ext_id.id':1, 'ext_id.type':1})

(a) indexes the object, (b) indexes parts of the object.

- Gates

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


gonzoprosperity

unread,
Apr 14, 2011, 7:48:52 PM4/14/11
to mongodb-user
> So it's not obvious right off the top, but these are actually two different
> queries:

Yes, I see that now. In my case its OK, as I only store and query off
those two fields. But yes, its important to remember that.

This is documented at

http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

under "Dot Notation vs. Subobjects" -

"To match only objects with these exact keys and values, we use an
object:

db.blog.findOne({"author" : {"name" : "Jane", "id" : 1}})
Note that

db.blog.findOne({"author" : {"name" : "Jane"}})

will not match, as subobjects have to match exactly (it would match an
object with one field: {"name" : "Jane"}). "

So my current take-away: use the declarative dot-notation version, so:

db.users.find({'ext_id.id':"6708526", 'ext_id.type':1}).explain()

Thanks for looking into this.








On Apr 14, 4:39 pm, Gaetan Voyer-Perrault <ga...@10gen.com> wrote:
> So it's not obvious right off the top, but these are actually two different
> queries:
>
> (1) db.users.find({'ext_id.id':"6708526", 'ext_id.type':1})
> (2) db.users.find({'ext_id':{'id':"6708526", 'type':1}})
>
> Query #1 would match the following document; #2 would not. (*note the extra
> field*)
> users : {
>   first_name: "Foo",
>   ext_id: {
>       id: "6708526",
>       type: 2,
>       name : 'test'
>   }
>
> }
>
> ===
> The following build two different indexes
> (a) db.users.ensureIndex({'ext_id <http://ext_id.id/>':1})

Gates

unread,
Apr 15, 2011, 5:19:39 PM4/15/11
to mongodb-user
To clarify, I believe there is a bug here as documented in the JIRA
ticket.

Even though both queries return different data, query (2) is a direct
subset of (1) and could definitely leverage the existing index.

I've added test script to the JIRA ticket.

- Gates

On Apr 14, 3:42 pm, gonzoprosperity <tool...@gmail.com> wrote:
> MongoDB 1.8.1
>
> My documents look like:
>
> users : {
>    first_name: "Foo",
>    ext_id: {
>        id: "6708526",
>        type: 2
>    }
>
> }
>
> I have a dual index on "ext_id.id"  and "ext_id.type".
>
> db.users.find({'ext_id.id':"6708526", 'ext_id.type':1}).explain()
>
> uses the index.
>
> However,
>
> db.users.find({'ext_id':{'id':"6708526", 'type':1}}).explain()
>
> Does not. Both return my expected document, though.
>
> From my understanding of the docs at:http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Obj...
Reply all
Reply to author
Forward
0 new messages