[英]Conditional $sort in aggregation
我想要對價格進行條件排序,數據庫中有兩種價格,所以我正在檢查sellType
是fixed price
還是bid
,
if(sellType===fixed price)
那么我想獲取正常價格,但是if(sellType===bid)
然后我想從存儲在數組中第 0 個 position 的 object 中獲取價格。
所以我嘗試進行聚合查詢,分享我的代碼片段
{
$addFields: {
sortedPrice: {
$cond: {
if: {
$eq: ["$itemDetail.sellType","fixed price"]
},
then: '$itemDetail.price',
else: '$itemDetail.bidInfo[0].price',
}
}
}
}
我已經嘗試了所有可能的解決方案,但無法從bidInfo
數組中獲取價格。 bidInfo
是一個對象數組,但我只想要第 0 個 position object (價格非常精確)
您可以嘗試使用$arrayElemAt
,
{
$addFields: {
sortedPrice: {
$cond: {
if: {
$eq: ['$itemDetail.sellType', 'fixed price'],
},
then: '$itemDetail.price',
else: { $arrayElemAt: ['$itemDetail.bidInfo.price', 0] },
},
},
},
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.