MongoDB 使用管道的聚合查找不起作用

MongoDB Aggregation Lookup with Pipeline Doesn't Work

我有两个collections。我正在尝试将Collection 2的文档添加到Collection 1,如果Collection 2中的数字1和数字2在Collection 1中指定的特定范围内。仅供参考 Collection 1 中的 ObjectId 和 Collection 2 中的 ObjectId 指的是两个不同的 items/products,因此我无法在此 ID 上加入两个 collections。

示例文档来自 Collection 1:

{'_id': ObjectId('4321'),
 'number1_lb': 61.205672407820025,
 'number1_ub': 61.24170844385606,
 'number2_lb': -149.75074963516136,
 'number2_ub': -149.71471359912533}

来自 Collection 2 的示例文档:

{'_id': ObjectId('1234'),
  'number1': 1.282298,
  'number2': 103.8475}

我想要输出:

{'_id': ObjectId('4321'),
 'number1_lb': 61.205672407820025,
 'number1_ub': 61.24170844385606,
 'number2_lb': -149.75074963516136,
 'number2_ub': -149.71471359912533,
 'recs': [ObjectId('3456'), ObjectId('4567'),...]

我认为带管道的查找阶段会起作用。目前我的代码如下:

 {"$lookup":{
        "from": "Collection 2",
        "let":{
            "number1_lb":"$number1_lb",
            "number1_ub":"$number1_ub",
            "number2_lb":"$number2_lb",
            "number2_ub":"$number2_ub"
        },
        "pipeline": [
            {"$match":
             {"$expr":
              {"$and":[
                  {"$gte":["$number1","$$number1_lb"]},
                  {"$gte":["$number2","$$number2_lb"]},
                  {"$lte":["$number1","$$number1_ub"]},
                  {"$lte":["$number2","$$number2_ub"]}
              ]}}}
        ],
        "as": "recs"
    }}

但是运行上面没有输出。我做错了什么吗??

您希望使用 $lookup$project :

   {
        $lookup: {
            from: "Collection2",
            localField: [Foreign Field of the Collection1],
            foreignField: [Principal field of the foreign collection here Collection2],
            as: "nameJoint"
            }
    },
    {$project: {

        "newFieldName": 
    }},

但是要在 2 个文档之间建立一个联合,这两个文档之间必须有一个公共字段。我不确定在这种情况下是否存在,或者我误解了它。

(A $lookup 基本上是 SQL 关节 SQL )

我 运行 它似乎工作正常;但我不得不调整 coll1 中的输入数据,因为它不符合 $match 标准。

from pymongo import MongoClient
from bson.json_util import dumps

db = MongoClient()["testdatabase"]
# Data Setup
db.coll1.replace_one({"_id": "4321"}, {"_id": "4321", "number1_lb": -61.205672407820025, "number1_ub": 61.24170844385606, "number2_lb": -149.75074963516136, "number2_ub": 149.71471359912533}, upsert=True)
db.coll2.replace_one({"_id": "1234"}, {"_id": "1234", "number1": 1.282298, "number2": 103.8475}, upsert=True)
# Run the aggregation
results = db.coll1.aggregate([
    {"$lookup": {
        "from": "coll2",
        "let": {
            "number1_lb": "$number1_lb",
            "number1_ub": "$number1_ub",
            "number2_lb": "$number2_lb",
            "number2_ub": "$number2_ub"
        },
        "pipeline": [
            {"$match":
                {"$expr":
                    {"$and": [
                        {"$gte": ["$number1", "$$number1_lb"]},
                        {"$gte": ["$number2", "$$number2_lb"]},
                        {"$lte": ["$number1", "$$number1_ub"]},
                        {"$lte": ["$number2", "$$number2_ub"]}
                    ]}}}
        ],
        "as": "recs"
    }}
])
# pretty up the results
print(dumps(results, indent=4))

给出:

[
    {
        "_id": "4321",
        "number1_lb": -61.205672407820025,
        "number1_ub": 61.24170844385606,
        "number2_lb": -149.75074963516136,
        "number2_ub": 149.71471359912533,
        "recs": [
            {
                "_id": "1234",
                "number1": 1.282298,
                "number2": 103.8475
            }
        ]
    }
]