【问题标题】:How to query a collection using count of a field in mongoDB using mongoose?如何使用 mongoose 使用 mongoDB 中的字段计数来查询集合?
【发布时间】:2021-09-18 13:40:37
【问题描述】:

我一直在尝试查询帖子文档。集合的查询如下。

db.getCollection('posts').find({ isActive: true, inappropriateReports: { '$exists': true, '$gt': { '$size': 0 } }})

我能够得到想要的结果,但问题是我一添加日期过滤器 它开始返回空数组作为响应的查询

db.getCollection('tribe-posts').find({ isActive: true,
  createdAt: {
    '$gte': '2021-06-01T00:00:00.000Z',
    '$lte': '2021-07-07T00:00:00.000Z'
  },
  inappropriateReports: { '$exists': true, '$gt': { '$size': 0 } }})

如何解决这个问题以及对没有“inappropriateReports”字段的集合进行空检查?

提前致谢

【问题讨论】:

    标签: node.js database mongodb mongoose filtering


    【解决方案1】:

    假设您正在查看部落帖子,请尝试以下操作:

    db.getCollection('tribe-posts').find({ isActive: true,
      createdAt: {
        '$gte': new Date('2021-06-01T00:00:00.000Z'),
        '$lte': new Date('2021-07-07T00:00:00.000Z')
      },
      inappropriateReports: { '$exists': true, '$gt': { '$size': 0 } }})
    

    我认为将日期作为字符串传递是行不通的。

    【讨论】:

      猜你喜欢
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 2017-10-19
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      相关资源
      最近更新 更多