Mongodb : ISODate格式的时间查询
Mongodb : Query based on time in ISODate format
假设Mongodb数据库中的样本文档如下:
{ "date" : ISODate("2015-11-09T05:58:19.474Z") }
{ "date" : ISODate("2014-10-25T07:30:00.241Z") }
{ "date" : ISODate("2015-11-30T15:24:00.251Z") }
{ "date" : ISODate("2012-01-10T18:36:00.101Z") }
预计:
{ "date" : ISODate("2015-11-09T05:58:19.474Z") }
{ "date" : ISODate("2014-10-25T07:30:00.241Z") }
我有兴趣查找 "date" 字段中的时间在 04:00 和 08:00 之间的文档,而不考虑日期、月份和年份。间接查询必须匹配日期字段中的任何 "YYYY-MM-DDT"。
我的方法是,从节点查询假定日期持续时间内的所有文档,然后对于与查询匹配的每个文档,将文档的 "date" 字段与 "yyyy-MM-DDT"+[= 进行比较29=]("YYYY-MM-DD is copied from each document's "日期字段“通过转换为 moment() 进行比较并获取月份、日期和年份”)使用 moment.js 模块。
有什么方法可以查询直接得到相同的结果吗?
注意:我正在使用 nodejs 连接到 mongodb
一种方法是使用 aggregation framework, in particular the $redact
operator which strips the document stream of content based on values within the document and its sub-documents. Depending on the result of a boolean expression, a document can be pruned from the stream, be included in the stream after its sub-documents have also been checked, or just passed complete into the stream. The idea behind $redact
以便轻松地从流中删除敏感信息。
在您的情况下,标准表达式使用 $cond
operator and the $and
boolean operator to express the logical AND between the time ranges with the comparison operators $gt
and $lt
. Use the $hour
日期运算符来 return date
字段的小时作为数字介于 0 和 23 之间。因此您的最终聚合如下所示:
db.collection.aggregate([
{
"$redact": {
"$cond": {
"if": {
"$and": [
{ "$gt": [ {"$hour": "$date"}, 4] },
{ "$lt": [ {"$hour": "$date"}, 8] }
]
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}
}
])
示例输出:
/* 0 */
{
"result" : [
{
"_id" : ObjectId("56404450472fe25cc6b85886"),
"date" : ISODate("2015-11-09T05:58:19.474Z")
},
{
"_id" : ObjectId("56404450472fe25cc6b85887"),
"date" : ISODate("2014-10-25T07:30:00.241Z")
}
],
"ok" : 1
}
假设Mongodb数据库中的样本文档如下:
{ "date" : ISODate("2015-11-09T05:58:19.474Z") }
{ "date" : ISODate("2014-10-25T07:30:00.241Z") }
{ "date" : ISODate("2015-11-30T15:24:00.251Z") }
{ "date" : ISODate("2012-01-10T18:36:00.101Z") }
预计:
{ "date" : ISODate("2015-11-09T05:58:19.474Z") }
{ "date" : ISODate("2014-10-25T07:30:00.241Z") }
我有兴趣查找 "date" 字段中的时间在 04:00 和 08:00 之间的文档,而不考虑日期、月份和年份。间接查询必须匹配日期字段中的任何 "YYYY-MM-DDT"。
我的方法是,从节点查询假定日期持续时间内的所有文档,然后对于与查询匹配的每个文档,将文档的 "date" 字段与 "yyyy-MM-DDT"+[= 进行比较29=]("YYYY-MM-DD is copied from each document's "日期字段“通过转换为 moment() 进行比较并获取月份、日期和年份”)使用 moment.js 模块。
有什么方法可以查询直接得到相同的结果吗?
注意:我正在使用 nodejs 连接到 mongodb
一种方法是使用 aggregation framework, in particular the $redact
operator which strips the document stream of content based on values within the document and its sub-documents. Depending on the result of a boolean expression, a document can be pruned from the stream, be included in the stream after its sub-documents have also been checked, or just passed complete into the stream. The idea behind $redact
以便轻松地从流中删除敏感信息。
在您的情况下,标准表达式使用 $cond
operator and the $and
boolean operator to express the logical AND between the time ranges with the comparison operators $gt
and $lt
. Use the $hour
日期运算符来 return date
字段的小时作为数字介于 0 和 23 之间。因此您的最终聚合如下所示:
db.collection.aggregate([
{
"$redact": {
"$cond": {
"if": {
"$and": [
{ "$gt": [ {"$hour": "$date"}, 4] },
{ "$lt": [ {"$hour": "$date"}, 8] }
]
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}
}
])
示例输出:
/* 0 */
{
"result" : [
{
"_id" : ObjectId("56404450472fe25cc6b85886"),
"date" : ISODate("2015-11-09T05:58:19.474Z")
},
{
"_id" : ObjectId("56404450472fe25cc6b85887"),
"date" : ISODate("2014-10-25T07:30:00.241Z")
}
],
"ok" : 1
}