【问题标题】:MongoDB Query: How to add some days to a date field before comparing it with a given dateMongoDB 查询:如何在将日期字段与给定日期进行比较之前添加一些天数
【发布时间】:2014-11-23 20:07:01
【问题描述】:

以下代码 sn -p 检索在给定日期之前已修改的所有用户:

val date = DateTime(...).getMillis

users.find(
  Json.obj("lastUpdate" -> Json.obj("$lt" -> Json.obj("$date" -> date))),
  None, None, page, perPage
)

如何找回从lastUpdate开始的一段时间内被修改的所有用户?换句话说,我需要在lastUpdate 中添加一些日期并将其与给定日期进行比较:

users.find(
  Json.obj("lastUpdate" -> /* how do I add N days to lastUpdate before comparing it with date? */
    Json.obj("$lt" -> Json.obj("$date" -> date))
  ),
  None, None, page, perPage
)

【问题讨论】:

    标签: mongodb scala mongodb-query reactivemongo


    【解决方案1】:

    你不能。 MongoDB 中的简单查询不能使用它正在查询的文档中的值。您只能在整个查询中使用不变的值。

    有效查询:

    {
        FirstName : "bar"
    }
    

    无效查询:

    {
        FirstName : LastName
    }
    

    但是,您可以通过其他方式实现这一目标,例如 MongoDB 的聚合框架

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多