[英]Node.js sorting array with findOne
我正在尝试按时间降序对 Mongodb、 Image
元素中的数组进行排序。 日期位于数组内。 这是当前的输出:
"address": "Product Address 1",
"town": "Brighton",
"Image": [
{
"price": 1021,
"time": "2020-09-13T12:06:34.079Z"
},
{
"price": 1022,
"time": "2020-09-13T12:18:50.270Z"
},
{
"price": 1023,
"time": "2020-09-13T12:37:22.499Z"
}
]
我首先需要的是以下元素,然后是剩余的两个数组元素:
{
"price": 1023,
"time": "2020-09-13T12:37:22.499Z"
}
我的代码是:
dbProduct.findOne({
productId: productId
},{'image.token':0}).then(dbRes => {
if (dbRes !== null) {
res.apiSuccess({
town: dbRes.town,
county: dbRes.county,
postCode: dbRes.postCode,
image: dbRes.image
});
} else {
res.apiError(messages.product.not_found);
}
任何帮助表示赞赏。 谢谢
试试这个,看看它是否有帮助
dbProduct.findOne({
productId: productId
},{'image.token':0},{sort: {image.$.time: -1 }}).then(dbRes => {
if (dbRes !== null) {
res.apiSuccess({
town: dbRes.town,
county: dbRes.county,
postCode: dbRes.postCode,
image: dbRes.image
});
} else {
res.apiError(messages.product.not_found);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.