我想在聚合中像这样进行过滤。这是 Mongoose 查询的示例。我想在聚合中做同样的事情。我想要做的是获取 2016-06-01T22:52:46Z 和 2016-06-02T22:52:46Z 之间的所有测量值以及其他值
Measurement
.find({})
.or([{
'date': {
'$gte': ISODate('2016-06-01T22:52:46Z'),
'$lte': ISODate('2016-06-02T22:52:46Z')
}
}, {
'date': {
'$gte': ISODate('2016-06-10T22:52:46Z'),
'$lte': ISODate('2016-06-11T22:52:46Z')
}
}, {
'date': {
'$gte': ISODate('2016-06-14T22:52:46Z'),
'$lte': ISODate('2016-06-15T22:52:46Z')
}
}, {
'date': {
'$gte': ISODate('2016-06-26T22:52:46Z'),
'$lte': ISODate('2016-06-27T22:52:46Z')
}
}])
.exec();
这是我尝试过的,但它没有做同样的工作。 IDE 会显示重复的键“日期”
db.measurements.aggregate([{
$match: {
$or: [{
date: {
'$gte': ISODate('2016-06-01T22:52:46Z'),
'$lte': ISODate('2016-06-02T22:52:46Z')
},
date: {
'$gte': ISODate('2016-06-10T22:52:46Z'),
'$lte': ISODate('2016-06-11T22:52:46Z')
},
date: {
'$gte': ISODate('2016-06-14T22:52:46Z'),
'$lte': ISODate('2016-06-15T22:52:46Z')
},
date: {
'$gte': ISODate('2016-06-26T22:52:46Z'),
'$lte': ISODate('2016-06-27T22:52:46Z')
}
}]
}
}]);
最佳答案
你有一些数据示例吗? 试试这个代码:
db.measurements.aggregate([{
$match: {
$or: [
{ date: {
'$gte': ISODate('2016-06-01T22:52:46Z'),
'$lte': ISODate('2016-06-02T22:52:46Z')
}},
{ date: {
'$gte': ISODate('2016-06-10T22:52:46Z'),
'$lte': ISODate('2016-06-11T22:52:46Z')
}},
{ date: {
'$gte': ISODate('2016-06-14T22:52:46Z'),
'$lte': ISODate('2016-06-15T22:52:46Z')
}},
{ date: {
'$gte': ISODate('2016-06-26T22:52:46Z'),
'$lte': ISODate('2016-06-27T22:52:46Z')
}}
]
}
}]);
关于node.js - Mongoose $聚合$匹配$或日期之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38359622/