【发布时间】:2020-06-21 15:35:53
【问题描述】:
我有sortValue,也就是"title-asc","title-desc","createdAt-asc","createdAt-desc",并拆分成sortVar(“title”,“createdAt”)和sortType(“asc ", "desc")
我想按 sortVar 和 sortType 条件对数据进行排序
所以我写了这样的聚合 ->
Product.aggregate([
...
{
$sort: {
$cond: {
if: {
$eq: [sortVar, "title"]
},
then: {
$cond: {
if: {
$eq: [sortType, "asc"]
},
then: {
createdAt: 1
},
else: {
createdAt: -1
}
}
},
else: {
}
}
}
},
{
$sort: {
$cond: {
if: {
$eq: [sortVar, "createdAt"]
},
then: {
$cond: {
if: {
$eq: [sortType, "asc"]
},
then: {
createdAt: 1
},
else: {
createdAt: -1
}
}
},
else: {
}
}
}
}
])
并返回错误$meta is the only expression supported by $sort right now...我是猫鼬的新手..所以我无法按条件排序..如果有人可以帮助我,我会很高兴)
【问题讨论】:
标签: node.js sorting mongoose aggregate