Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering a collection in Meteor.js by a date range or word filter

I have a collection in Meteor.js with properties which include a timestamp. For example:

Posts.insert({
    category: 'type1',
    text: 'hello world',
    time: new Date(2012, 2, 14, 15, 25),
});

I know I can filter the Collection by matching a parameter, e.g.

    Meteor.subscribe('posts', 'type1');

    Meteor.publish('posts', function(category) {
        return Posts.find({category: category});
    });

However, I want to also be able to filter in more advanced ways: 1) by the "time" field, e.g. all posts in between Jan 1, 2012 and Jan 1, 2013. 2) by searching for all posts which have some word, e.g. "world" in the "text" field.

What is the correct way to do this?

like image 301
genekogan Avatar asked Nov 30 '25 20:11

genekogan


1 Answers

You can just combine selectors like so:

Posts.find({
  category: category,
  time: {$gte: date1, $lte: date2},
  text: new RegExp(searchTerm)
});
like image 120
David Weldon Avatar answered Dec 03 '25 11:12

David Weldon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!