【发布时间】:2018-01-10 01:58:56
【问题描述】:
我有一个带有 $facet 的查询构造,它以这种格式返回结果:
{price_now: [
{ _id: 'apple', price_now: 1.02 },
{ _id: 'melon', price_now: 3.18 },
{ _id: 'cherry', price_now: 2.57 }],
price_15m: [
{ _id: 'apple', price_15m: 1.08 },
{ _id: 'melon', price_15m: 3.12 },
{ _id: 'cherry', price_15m: 2.82 }],
price_30m: [
{ _id: 'apple', price_30m: 1.05 },
{ _id: 'melon', price_30m: 3.04 },
{ _id: 'cherry', price_30m: 2.94 }]
我怎样才能得到这种格式的结果?:
{ _id: 'apple', price_now: 1.02, price_15m: 1.08, price_30m: 1.05 },
{ _id: 'melon', price_now: 3.18, price_15m: 3.12, price_30m: 3.04 },
{ _id: 'cherry', price_now: 2.57, price_15m: 2.82, price_30m: 2.94 }
完整的查询如下所示:
var today = new Date();
var shift_15m = new Date(today.getTime()-15*60*1000);
var shift_30m = new Date(today.getTime()-30*60*1000);
food.aggregate( [
{
$facet: {
price_now: [
{
$group: {
_id: "$name",
price_now: {$last: "$price"}
}
}
],
price_15m: [
{
$match: {
date: { "$lte": shift_15m }
}
}, {
$group: {
_id: "$name",
price_5m: {$last: "$price"}
}
}
],
price_30m: [
{
$match: {
date: { "$lte": shift_30m }
}
}, {
$group: {
_id: "$name",
price_30m: {$last: "$price"}
}
}
]
}
}
])
我需要在一个查询中获取不同时间间隔的每种产品的价格。也许您知道一种更好更快的方法来获取不同时间的价格。 我的 MongoDB 版本是 v3.6.0。我在 Node.js 应用程序中使用猫鼬。
【问题讨论】:
-
在
$facet之前添加一个$match阶段,以限制今天和您的最高范围之间的产品。您是否总是在每个时间类别中返回固定数量的产品?只是试图理解,以便我们可以格式化它们。 -
@Veeram 我只存储最近 6 小时的价格。有时可以将新产品添加到数据库中。
-
好的,我已经添加了答案。看看对你有没有帮助。
标签: mongodb mongoose aggregation-framework