【问题标题】:Search Date Range in Meteor在 Meteor 中搜索日期范围
【发布时间】:2014-06-30 01:00:37
【问题描述】:

我在 Meteor 中为一个项目做一个搜索系统,需要在两个日期之间放置一个字段来搜索。但是,日期在 MongoDB 的数组中:

"relatorios" : [
    {
        "mes" : ISODate("2013-11-01T02:00:00Z"),
        "revistas" : "2",
        "brochuras" : "2",
        "livros" : "0",
        "folhetos" : "0",
        "revisitas" : "0",
        "estudos" : "0",
        "horas" : "12"
    },
    {
        "mes" : ISODate("2013-09-01T03:00:00Z"),
        "revistas" : "0",
        "brochuras" : "0",
        "livros" : "0",
        "folhetos" : "0",
        "revisitas" : "0",
        "estudos" : "0",
        "horas" : "12"
    }
]

我曾尝试通过 mongo 直接查询过滤日期,但不能。我在一些论坛上阅读了有关在 Meteor 中使用 MapReduce 的信息。 什么是最好的选择?如果可能的话,我该怎么做?

【问题讨论】:

  • 你能澄清你的问题吗?我不确定你在问什么。
  • 我需要通过 Meteor 查询子文档 mongo 中的日期。

标签: mongodb meteor


【解决方案1】:

您可以使用点表示法,例如 a 和 b 之间的两个日期

var start = new Date(450000);
var end = new Date(5450000000000);

CollectionName.find({ 'relatorios.mes' : { $gte : start, $lt: end });

所以这将获得所有具有与该字段匹配的数组的文档。请记住 mongodb 提取文档,因此如果您只有一个匹配的数组,它将为您提供整个文档。

【讨论】:

  • 我是这样做的,但是 mongo 返回了数组的所有元素,而不仅仅是日期。他不可能只返回与查询匹配的元素吗?或者我如何只找到匹配的元素?
  • 我明白了!我正在删除与直接 Meteor 中的日期不匹配的元素。谢谢!
  • 我认为应该是CollectionName.find({ 'relatorios.mes' : { $gte : start, $lt: end }}); 注意额外的}
【解决方案2】:

您可以使用 $elemMatch 查找在两个日期之间具有 mes 值的单个数组 relatorios 元素,并使用 $ 位置投影运算符仅在输出中包含该匹配元素。

在外壳中:

start = new Date('2013-11-01T01:30:00Z');
end = new Date('2013-11-01T02:30:00Z');
db.test.find(
    {relatorios: {$elemMatch: {mes: {$gt: start, $lt: end}}}}, 
    {'relatorios.$': 1})

如果您不使用$elemMatch,那么它可以将$gt 与一个元素匹配,将$lt 与另一个元素匹配。

或者如果您需要获取日期范围内的所有 relatorios 元素(而不仅仅是第一个),您可以使用aggregate

db.test.aggregate([
    // Get all docs with at least one element in the date range
    {$match: {relatorios: {$elemMatch: {mes: {$gt: start, $lt: end}}}}}, 
    // Duplicate each doc, once per relatorios element.
    {$unwind: '$relatorios'}, 
    // Filter those to just the ones in the date range.
    {$match: {'relatorios.mes': {$gt: start, $lt: end}}}
])

【讨论】:

  • 据我所知,Meteor 不支持聚合
  • @MuriloVenturoso 似乎确实有解决方法:stackoverflow.com/a/18884223/1259510
  • 我正在运行您传递的代码,但 Meteor 返回以下错误:Object [object Object] has no method 'aggregate'
  • 如果你添加了诸如meteorhacks:aggregate 包(atmospherejs.com/meteorhacks/aggregate)之类的东西,那么你只能在服务器端进行聚合。尝试在客户端执行此操作或尝试在没有聚合包的情况下执行此操作会给您这样的错误。
【解决方案3】:
    let date = new Date();
    let getFirstDay = new Date(date.getFullYear(), date.getMonth() + 1, 1);
    let getLastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

    let firstDay = getFirstDay.getFullYear() + '-' + ((''+getFirstDay.getMonth()).length<2 ? '0' : '') + getFirstDay.getMonth() + '-' + ((''+getFirstDay.getDate()).length<2 ? '0' : '') + getFirstDay.getDate()+"T00:00:00.000Z";
    let lastDay = getLastDay.getFullYear() + '-' + ((''+getLastDay.getMonth()).length<2 ? '0' : '') + (getLastDay.getMonth()+1) + '-' + ((''+getLastDay.getDate()).length<2 ? '0' : '') + getLastDay.getDate()+"T00:00:00.000Z";

var pipeline = [
        { 
            $match: { headofficeId: headofficeId }},
            { $match: {'createdDate': {$gt: new Date(firstDay), $lt: new Date(lastDay)}}},
            {$group: {_id: "$branchId", total: {$sum: "$actualValue"}}},
            { $sort: { total: -1 } 
        }
    ];
var result = Commissions.aggregate(pipeline);
return result;

【讨论】:

  • 这与您的代码不匹配。但这是一个工作代码,您可以根据需要使用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多