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

时间:2019-12-20 13:39:40

标签: mongodb pymongo aggregation

我有两个收藏。我正在尝试将集合2的文档添加到集合1中,如果集合2中的1号和2号在集合1中指定的某个范围内。集合1中的FYI ObjectId和集合2中的ObjectId引用两个不同的项/产品,因此我无法加入此ID上的两个收藏夹。

集合1中的示例文档

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

集合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"
    }}

但是运行上面的命令没有任何输出。我做错什么了吗?

2 个答案:

答案 0 :(得分:1)

我运行了它,看起来效果很好;但是我不得不在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
            }
        ]
    }
]

答案 1 :(得分:0)

您要使用$lookup$project

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

        "newFieldName": 
    }},

但是要在两个文档之间建立一个联合,在这两个文档之间要有一个公共字段。我不确定这种情况是否会发生,否则我会误解。

$lookup是noSQL中的SQL联合)