【问题标题】:How to get all user ids from a collection where the document not present in another collection如何从文档不存在于另一个集合中的集合中获取所有用户 ID
【发布时间】:2021-03-23 04:55:55
【问题描述】:

col1:

{
 "uuid" : "aaa"
}
{
 "uuid" : "bbb"
}
{
 "uuid" : "ccc"
}

col2:

{
 "uuid" : "aaa"
 "offer_id" : "offer_id"
}

我需要一个查询来从 col1 获取所有 UUID,其中 UUID 不会显示特定的商品 ID。 (offer_id 将被硬编码)

预期输出:

{
 "uuids" : ["bbb","ccc"]
}

【问题讨论】:

  • 请发布预期输出。

标签: python mongodb mongodb-query pymongo lookup


【解决方案1】:

试试这个:

db.col1.aggregate([
    {
        $lookup: {
            from: "col2",
            let: { uuid: "$uuid" },
            pipeline: [
                {
                    $match: {
                        $expr: { $ne: ["$uuid", "$$uuid"] }
                    }
                }
            ],
            as: "uuids"
        }
    },
    { $unwind: "$uuids" },
    {
        $group: {
            _id: null,
            uuids: {
                $push: "$uuid"
            }
        }
    }
])

输出

{
    "_id" : null,
    "uuids" : [
        "bbb",
        "ccc"
    ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2021-10-30
    • 2019-02-05
    • 2021-07-17
    • 2020-08-09
    • 2021-06-29
    • 2021-01-12
    相关资源
    最近更新 更多