Meteor.js - 使用提示使Mongo使用索引?

时间:2015-02-25 23:20:18

标签: meteor

我使用Meteor来查询MongoDB集合。该查询当前使用了错误的索引。使用原始Mongo,可以将hint传递给查询以使用指定的索引。有没有办法直接从Meteor中做到这一点?

2 个答案:

答案 0 :(得分:4)

这可以使用$ query:$ hint:语法直接在Meteor内部完成。值得注意的是,使用sort选项而不是$ orderBy:似乎会导致Meteor抱怨。

示例:

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

确保您在提示中指定的索引实际存在于您的数据库中,否则mongo会抱怨收到错误的提示。

答案 1 :(得分:1)

快速浏览一下meteor-mongo warpper代码,我看不到它。 但是,整个node.js驱动程序可用于meteor(请参阅findAndModify https://github.com/meteor/meteor/issues/1070的类似问题)。

而且,提示在node.js驱动程序中是绝对可用的,因此构建自己的包装器可能是解决方案吗?我还没有必要越过这座桥,所以我无法提供更明确的解决方案,但这可能是一个很好的起点(对于一个包来说是一个好主意!)。