Multiple indexes with hint

2,271 views
Skip to first unread message

Rhys Campbell

unread,
Dec 4, 2015, 10:05:41 AM12/4/15
to mongodb-user

Hello All,

I've been looking at a problem Mongo Query and was attempting to specify multiple indexes using the hint(). This lead me onto the following...



So I guess this still isn't possible but I'd like a clarification is anyone know...

I have a query with a couple of or clauses that would ideally use a different, specific index each. One of them sometimes doesn't, if I specify the index using hint, i.e.

db.collection.find( { "$or": [ { "foo": 1}, { "bar": 2 } ]  }).hint( "index_on_bar");


Would this restrict the first clause to that index only? In my case that index is not at all useful for the first or clause so I think it's causing a collection scan.

I have attempted something like this..

db.collection.find( { "$or": [ { "foo": 1}, { "bar": 2 } ]  }).hint( "index_on_foo", "index_on_bar");




The syntax is accepted but the query doesn;t complete.

Cheers,

Rhys

Asya Kamsky

unread,
Dec 5, 2015, 12:58:03 AM12/5/15
to mongodb-user
There is no way to hint this, but you can use index filters feature in this case.


Asya


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/db3abe9b-5db3-4132-9cb0-7492e185c198%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers

Rhys Campbell

unread,
Dec 15, 2015, 2:58:20 AM12/15/15
to mongodb-user
Thanks. I'll take a look at this.

Rhys

Asya Kamsky

unread,
Dec 15, 2015, 10:45:45 AM12/15/15
to mongodb-user
Note that it won't help you prevent a collection scan - if your first "$or" clause has no useful index, then the only way to find those documents is via collection scan, right?

An "$or" is a union of the two sets - when optimizing $or think of the two clauses as two separate queries - your full query will be in the ballpark of the slower of the two of them.

Asya


On Mon, Dec 14, 2015 at 11:58 PM, Rhys Campbell <rhys.jame...@gmail.com> wrote:
Thanks. I'll take a look at this.

Rhys

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages