Meteor正则表达式查找()比MongoDB控制台慢得多

时间:2015-12-02 15:08:41

标签: regex mongodb meteor

我过去两周一直在研究A LOT,并且无法确定我的Meteor应用程序返回结果太慢的确切原因。

目前,我的Mongo数据库中只有一个集合,包含大约2,00,000个文档。并且搜索我在给定关键字的基础上使用Meteor订阅。这是我的疑问:

db.collection.find({$or:[
{title:{$regex:".*java.*", $options:"i"}}, 
{company:{$regex:".*java.*", $options:"i"}}
]})

当我在mongo shell中执行上述查询时,会立即返回结果。但是当我在Meteor客户端中使用它时,结果需要大约40秒才能从服务器返回。这是我的流星客户端代码:

Template.testing.onCreated(function () {
    var instance = this;
    // initialize the reactive variables
    instance.loaded = new ReactiveVar(0);
    instance.limit = new ReactiveVar(20);
    instance.autorun(function () {

    // get the limit
    var limit = instance.limit.get();
    var keyword = Router.current().params.query.k;
    var searchByLocation = Router.current().params.query.l;
    var startDate = Session.get("startDate");
    var endDate = Session.get("endDate");

    // subscribe to the posts publication
    var subscription = instance.subscribe('sub_testing', limit,keyword,searchByLocation,startDate,endDate);
    // if subscription is ready, set limit to newLimit
    $('#searchbutton').val('Searching');
    if (subscription.ready()) {
        $('#searchbutton').val('Search');
        instance.loaded.set(limit);
    } else {
        console.log("> Subscription is not ready yet. \n\n");
    }
});

instance.testing = function() {
    return Collection.find({}, {sort:{id:-1},limit: instance.loaded.get()});
}

这是我的流星服务器代码:

Meteor.publish('sub_testing', function(limit,keyword,searchByLocation,startDate,endDate) {
    Meteor._sleepForMs(200);
    var pat = ".*" + keyword + ".*";
    var pat2 = ".*" + searchByLocation + ".*";

    return Jobstesting.find({$or:[{title:{$regex: pat, $options:"i"}}, { company:{$regex:pat,$options:"i"}},{ description:{$regex:pat,$options:"i"}},{location:{$regex:pat2,$options:"i"}},{country:{$regex:pat2,$options:"i"}}],$and:[{date_posted: { $gte : endDate, $lt: startDate }},{sort:{date_posted:-1},limit: limit,skip: limit});
});

我还想在此提及我使用"Load More"分页,默认情况下limit参数获得20条记录。在每次"Load More"点击时,我将limit参数增加20,因此首次点击它是20,第二次点击40依此类推......

如果我出错了任何帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

  

但是当我在Meteor客户端中使用它时,结果需要大约40秒才能从服务器返回。

您可能误解了Meteor如何访问您的数据。

在客户端上运行在客户端上运行的查询。

  • Meteor.publish - 在服务器上提供数据
  • Meteor.subscribe - 将数据从服务器下载到客户端。
  • Collection.find - 查看客户端上的数据。

如果您认为Meteor方面很慢,您应该在服务器端(在之前/之后打印时间)计时并提交错误。

如果您正在实施寻呼机,您可以尝试使用流星方法,或者 一个pager package

相关问题