【问题标题】:How to get all array items that don't exists in a specific attribute in all documents of a collection in mongodb如何获取mongodb集合的所有文档中特定属性中不存在的所有数组项
【发布时间】:2021-03-19 09:33:03
【问题描述】:

我是 Nodejs 和 MongoDB 的新手。在我的一项任务中,我有一个动态的电话号码数组,如下所示:

var INPUT_ARRAY = [ "1000000000", "1000000050", "1000000100", "1000000101" ];

这个数组将包含 2k-3k 个值。我还有一个集合employees,其中包含大约 100000 个文档的员工详细信息,如下所示:

{
  "_id": {
    "$oid": "60536cc1aafebb0d10399df8"
  },
  "phone": "1000000000",
  "first_name": "john",
  "last_name": "doe"
}

我需要将动态数组值与员工集合进行匹配,并且需要从动态数组中返回员工集合中不存在的所有电话号码。

我为此搜索了很多,还检查了以下问题 Compare arrays and Return the Difference MongoDB Find values passed in that don't match

但这些问题的答案对我不起作用。

有人可以帮我找到一个性能更好的解决方案吗?

提前致谢。

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    解决方案 #1:

    var INPUT_ARRAY = ["1000000000", "1000000050", "1000000100", "1000000101"]
    
    db.employees.aggregate([
        {
            $group: {
                _id: null,
                phone_numbers: { $push: "$phone" }
            }
        },
        {
            $addFields: {
                missingNumbers: {
                    $filter: {
                        input: INPUT_ARRAY,
                        as: "phone_number",
                        cond: {
                            $not: {
                                $in: ["$$phone_number", "$phone_numbers"]
                            }
                        }
                    }
                }
            }
        }
    ]);
    

    解决方案#2:正如@Joe 在 cmets 中所建议的那样:

    var INPUT_ARRAY = ["1000000000", "1000000050", "1000000100", "1000000101"]
    
    db.employees.aggregate([
        {
            $group: {
                _id: null,
                phone_numbers: { $push: "$phone" }
            }
        },
        {
            $addFields: {
                missingNumbers: {
                    $setDifference: [INPUT_ARRAY, "$phone_numbers"]
                }
            }
        }
    ]);
    

    输出:

    {
        "_id" : null,
        "phone_numbers" : [
            "1000000000",
            "1000000050",
            "10000060"
        ],
        "missingNumbers" : [
            "1000000100",
            "1000000101"
        ]
    }
    

    employees收藏

    /* 1 createdAt:3/18/2021, 8:37:45 PM*/
    {
        "_id" : ObjectId("60536cc1aafebb0d10399df8"),
        "phone" : "1000000000",
        "first_name" : "john",
        "last_name" : "doe"
    },
    
    /* 2 createdAt:3/18/2021, 8:37:45 PM*/
    {
        "_id" : ObjectId("60536cc1aafebb0d10399df9"),
        "phone" : "1000000050",
        "first_name" : "jane",
        "last_name" : "doe"
    },
    
    /* 3 createdAt:3/18/2021, 8:37:45 PM*/
    {
        "_id" : ObjectId("60536cc1aafebb0d10399dfa"),
        "phone" : "10000060",
        "first_name" : "joe",
        "last_name" : "doe"
    }
    

    【讨论】:

    • 可以用$setDifference代替过滤器吗?
    猜你喜欢
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2020-10-02
    • 2023-03-28
    相关资源
    最近更新 更多