Mongodb 如何按数组中最后一项的时间筛选记录
每个记录都有历史记录数组,如何获取历史记录中项目的“更新时间”应>=3小时前的最后一个项目 我想在聚合中的预处理步骤之后执行此筛选 谢谢 文件格式Mongodb 如何按数组中最后一项的时间筛选记录,mongodb,Mongodb,每个记录都有历史记录数组,如何获取历史记录中项目的“更新时间”应>=3小时前的最后一个项目 我想在聚合中的预处理步骤之后执行此筛选 谢谢 文件格式 执行$REWIND,然后在必要时重新组合 collection.aggregate( [ { $unwind: "history" }, { $match: { "history.updated_at" : { $gt: ISODate("2015-09-17T10
执行$REWIND,然后在必要时重新组合
collection.aggregate( [
{ $unwind: "history" },
{
$match: {
"history.updated_at" : {
$gt: ISODate("2015-09-17T10:00:00Z")
}
}
},
{ $group: {
_id : {
_id : "$_id",
[ any fields except history ]
},
last_updated: { $last : "$history.updated_at"},
last_price: { $last : "$history.price"}
}}
])
可能重复的
"price": 2988.0,
"history": [
{
"updated_at": new Date(1441469169200),
"price": 2988
},
{
"updated_at": new Date(1441620011237),
"price": 1558
},
{
"updated_at": new Date(1441621596776),
"price": 2988
},
{
"updated_at": new Date(1441625059995),
"price": 1558
},
{
"updated_at": new Date(1441689121096),
"price": 2988
},
{
"updated_at": new Date(1441690782988),
"price": 1558
},
{
"updated_at": new Date(1441695110834),
"price": 2988
}
],
collection.aggregate( [
{ $unwind: "history" },
{
$match: {
"history.updated_at" : {
$gt: ISODate("2015-09-17T10:00:00Z")
}
}
},
{ $group: {
_id : {
_id : "$_id",
[ any fields except history ]
},
last_updated: { $last : "$history.updated_at"},
last_price: { $last : "$history.price"}
}}
])