Andrew F
Andrew F

Reputation: 595

Meteor.js - Use hint to make Mongo use index?

I'm using Meteor to query a MongoDB collection. The query is currently using the wrong index. With raw Mongo, it is possible to pass a hint to a query to use a specified index. Is there any way to do this from within Meteor directly?

Upvotes: 3

Views: 683

Answers (2)

challett
challett

Reputation: 906

This can be done directly inside of Meteor using the $query: $hint: syntax. It is worth noting that using the sort option instead of $orderBy: seems to cause Meteor to complain.

Example:

Meteor.collection.find( 
    {  $query:{
        //query goes here
    }, $hint: {
        "indexField1": 1, "indexField2": 1, "indexField3": -1
    }, $orderBy:{
        "createdAt": -1 //sorting option
    }
},
    {limit:30} //sort here makes Meteor complain
);

Make sure that the index that you are specifying in your hint actually exists in your db or else mongo will complain about receiving a bad hint.

Upvotes: 4

Matt K
Matt K

Reputation: 4948

From a quick glance at the meteor-mongo warpper code, I don't see it. However, the entire node.js driver is available to meteor (see a similar problem with findAndModify https://github.com/meteor/meteor/issues/1070).

And, hint is definitely available in the node.js driver, so perhaps building your own wrapper would be the solution? I haven't had to cross this bridge yet, so I can't offer a more explicit solution, but this may be a good starting point (and a great idea for a package!).

Upvotes: 1

Related Questions