我有两个集合,我们称它们为集合 A 和集合 B,它们都有价格字段和商品编号。所以假设每个都有 2 个项目:
集合 A:
name = item1 , price = 30
name = item2 , price = 20
集合 B:
name = item1 , price = 50
name = item2 , price = 10
我想显示集合 A 的价格高于集合 B 的价格的项目。对于上面的示例,例如 item2。
如何编写这样的查询?我正在使用 robomongo。
(编辑)
跟进我之前的问题;
我想显示集合 A 的价格大于集合 B 的价格 *(汇率)+(某个常数)的项目
假设条件是;
where A's price > B's price*1.5+4
所以在这种情况下它应该仍然显示相同的项目。
假设条件是;
where A's price > B's price*1.5+10
那么它不应该显示任何项目
最佳答案
您可以使用 $lookup
运算符首先对 collectionA
中的 collectionB
进行连接,将结果返回的单个元素数组展平为 $unwind
然后使用 $redact
管道进行文档级别的修订,其中文档
使用 $$KEEP
保留符合指定条件的系统变量,不满足条件的使用 $$PRUNE
丢弃强>。最后,您需要运行以下聚合管道:
var exchangeRate = 1.5;
db.collectionA.aggregate([
{ "$match": { "price": { "$exists": true }, "name": { "$exists": true } } },
{
"$lookup": {
"from": "collectionB",
"localField": "name",
"foreignField": "name",
"as": "collectionB"
}
},
{ "$unwind": "$collectionB" },
{
"$redact": {
"$cond": [
{
"$gt": [
"$price", {
"$add": [
{ "$multiply": [ "$collectionB.price", exchangeRate ] },
4
]
}
]
},
"$$KEEP",
"$$PRUNE"
]
}
}
])
Robomongo 演示
填充测试集合:
db.collectionA.insert([
{ "name": "item1", "price": 30 },
{ "name": "item2", "price": 20 }
])
db.collectionB.insert([
{ "name": "item1", "price": 50 },
{ "name": "item2", "price": 10 }
])
示例输出:
{
"_id" : ObjectId("58ad50a429b2961777f91c95"),
"name" : "item2",
"price" : 20,
"collectionB" : {
"_id" : ObjectId("58ad50a429b2961777f91c97"),
"name" : "item2",
"price" : 10
}
}
关于python - 如何聚合两个集合,其中一个集合的字段大于另一个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42384199/