简体   繁体   English

流星和MongoDB。 从收集中获取数据的麻烦(在两个日期之间查找文档)

[英]Meteor & MongoDB. Troubles with getting data from collection (finding documents between two dates)

I'm trying to get some documents from collection located between two dates(with 5 hour difference for example). 我正在尝试从位于两个日期之间的集合中获取一些文档(例如,相差5小时)。 My code: 我的代码:

Session.setDefault('sortBy', 'time');
    Template.content.helpers({
        posts: function() {
            if( Session.equals('sortBy', 'time') ){
                return Posts.find({author: Meteor.user().username}, {sort: {date_created: -1}});
            } 
            else if( Session.equals('sortBy', 'hours') ) {
                var endDate = new Date();
                var startDate = new Date(endDate);
                startDate.setHours(endDate.getHours()-5);
                console.log("startDate: " + startDate);
                console.log("endDate: " + endDate);

                return Posts.find({author: Meteor.user().username}, {date_created: {$gte: startDate,$lt: endDate}})
            }
        }
    });
    Template.content.events({
        'click .lasthour': function(e) {
            return Session.set('sortBy', 'hours');
        },
        'click .nosort': function(e) {
            return Session.set('sortBy', 'nosort');
        }
    });

But it always returns all documents (so filter not working). 但是它总是返回所有文档(因此过滤器不起作用)。 And problem not in Session variable 'sortBy', it working fine. 问题不在会话变量“ sortBy”中,它工作正常。

Your query is formatted wrong. 您的查询格式错误。 Should look like this: 应该看起来像这样:

return Posts.find(
  {
    author: Meteor.user().username,
    date_created: {$gte: startDate, $lt: endDate}
  }
);

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

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