如果我有一个名为 game 的集合,它具有这样的架构。
{"team": "A", "opponent": "B", "points": 101, "opp_points": 100}
{"team": "B", "opponent": "A", "points": 100, "opp_points": 101}
有没有办法查询同一集合中两个字段之间的比较?
等效的 SQL 是。
SELECT * FROM game where points > opp_points;
我查看了 Mongo 的文档并没有找到这个,但我认为他们可能有。
最佳答案
在聚合或 $find
中的 $match
中使用 $expr
db.collection.aggregate([
{
$match: {
$expr: {
$gt: [
"$points",
"$opp_points"
]
}
}
}
])
工作 Mongo playground聚合
db.collection.find({
$expr: {
$gt: [
"$points",
"$opp_points"
]
}
})
工作 Mongo playground的发现
关于mongodb - 有没有办法比较 MongoDB 查询中的两个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66405856/