我想检索MongoDB中过去7天特定时间段(下午3点至6点)的记录。

huangapple go评论56阅读模式
英文:

I want to retrieve the records in Mongodb for last 7 days for specific time. Like from 3-6 pm for last 7 days

问题

我想要一个查询,在过去的7天内获取事件发生在下午3点到6点之间的记录。

时间戳是Unix格式,所以我尝试了以下代码:

{ $expr: { $gte: [ { $hour: "$DateTime" }, 15 ], $lte: [ { $hour: "$DateTime" }, 18 ]} }

错误信息:表示表达式的对象必须正好有一个字段:{ $gte: [ { $hour: "$DateTime" }, 15 ], $lte: [ { $hour: "$DateTime" }, 18 ] }

任何帮助将不胜感激。

英文:

I want a query where I can get records for the last 7 days where events occurred between 3-6 pm

And timestamp is in unix so I tried

{ $expr: { $gte: [ { $hour: "$DateTime" }, 8 ], $lte: [ { $hour: "$DateTime" }, 18 ]} }

Error: An object representing an expression must have exactly one field: { $gte: [ { $hour: "$DateTime" }, 8 ], $lte: [ { $hour: "$DateTime" }, 18 ] }

Any help would be appreciated.

答案1

得分: 1

你必须使用 $and

db.collection.aggregate([
   {
      $match: {
         $expr: {
            $and: [
               { $gte: [{ $hour: "$DateTime" }, 8] },
               { $lte: [{ $hour: "$DateTime" }, 18] }
            ]
         }
      }
   }
])
英文:

You must use $and:

db.collection.aggregate([
   {
      $match: {
         $expr: {
            $and: [
               { $gte: [{ $hour: "$DateTime" }, 8] },
               { $lte: [{ $hour: "$DateTime" }, 18] }
            ]
         }
      }
   }
])

huangapple
  • 本文由 发表于 2023年6月8日 19:25:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定