我嘗試聚合 firstValue 減去 secondValue 大于 2 的檔案
示例檔案:
[
{
"firstValue": 6,
"secondValue": 3
},
{
"firstValue": 6,
"secondValue": 5
},
{
"firstValue": 6,
"secondValue": 7
}
]
查詢應該只回傳第一個元素 (6-3 = 3, 3>2)
uj5u.com熱心網友回復:
您可以使用簡單的 find :
db.collection.find({
"$expr": {
$gt: [
{
"$subtract": [
"$firstValue",
"$secondValue"
]
},
2
]
}
})
找小提琴
或將此運算式用于聚合:
db.collection.aggregate({
"$match": {
"$expr": {
$gt: [
{
"$subtract": [
"$firstValue",
"$secondValue"
]
},
2
]
}
}
})
聚合小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/396464.html
