英文:
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] }
]
}
}
}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论