【问题标题】:I would like to search by filtering by date in mongodb我想通过在 mongodb 中按日期过滤来搜索
【发布时间】:2018-12-24 04:43:37
【问题描述】:

我想获得没有“SENT”状态的姓名总数,我还想根据开始和结束日期过滤这个数字,目前只有第一部分走过它标志着按日期过滤。我的领域被称为“日期”。 抱歉我的代码没有缩进):

db.name.aggregate([{ $group: {_id:"$id", sent: 
{$max: {$cond: {if: { 
$eq: [ "$status", "SENT" ] }, then: 1, else: 0}}} } },
{ $match: { sent: 
0 } }, {$count: "total"}])

【问题讨论】:

    标签: database mongodb mongoose


    【解决方案1】:

    您可以重写查询以添加 $match 作为第一阶段,并包括日期和状态过滤器,然后是 $count 阶段以计算匹配的文档。

    类似

    db.name.aggregate([
        {"$match": {"status":"SENT","date":{"$gte":input date1, "$lte":input date2} }
        {"$count": "total"} 
    ])
    

    您不需要对请求进行聚合。您可以使用常规查询。

    db.name.count({"status":"SENT","date":{"$gte":input date1, "$lte":input date2} })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多