简体   繁体   English

通过日期范围或文字过滤器过滤Meteor.js中的集合

[英]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. 我在Meteor.js中有一个包含时间戳的属性的集合。 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, eg 我知道我可以通过匹配参数来过滤Collection,例如

    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, eg all posts in between Jan 1, 2012 and Jan 1, 2013. 2) by searching for all posts which have some word, eg "world" in the "text" field. 但是,我希望能够以更高级的方式过滤:1)通过“时间”字段,例如2012年1月1日到2013年1月1日之间的所有帖子.2)通过搜索所有有一些单词的帖子,例如“文本”字段中的“世界”。

What is the correct way to do this? 这样做的正确方法是什么?

You can just combine selectors like so: 您可以像这样组合选择器

Posts.find({
  category: category,
  time: {$gte: date1, $lte: date2},
  text: new RegExp(searchTerm)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM