khalid
khalid

Reputation: 377

Meteor regex find() far slower than in MongoDB console

I've been researching A LOT for past 2 weeks and can't pinpoint the exact reason of my Meteor app returning results too slow.

Currently I have only a single collection in my Mongo database with around 2,00,000 documents. And to search I am using Meteor subscriptions on the basis of a given keyword. Here is my query:

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

When I run above query in mongo shell, the results are returned instantly. But when I use it in Meteor client, the results take almost 40 seconds to return from server. Here is my meteor client code:

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

And here is my meteor server code:

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

One point I'd also like to mention here that I use "Load More" pagination and by default the limit parameter gets 20 records. On each "Load More" click, I increment the limit parameter by 20 so on first click it is 20, on second click 40 and so on...

Any help where I'm going wrong would be appreciated.

Upvotes: 1

Views: 213

Answers (1)

Michael Cole
Michael Cole

Reputation: 16227

But when I use it in Meteor client, the results take almost 40 seconds to return from server.

You may be misunderstanding how Meteor is accessing your data.

Queries run on the client are processed on the client.

  • Meteor.publish - Makes data available on the server
  • Meteor.subscribe - Downloads that data from the server to the client.
  • Collection.find - Looks through the data on the client.

If you think the Meteor side is slow, you should time it server side (print time before/after) and file a bug.

If you're implementing a pager, you might try a meteor method instead, or a pager package.

Upvotes: 1

Related Questions