【问题标题】:Find duplicates that are not null mongodb [duplicate]查找不为空的重复项 mongodb [重复]
【发布时间】:2015-12-09 16:20:17
【问题描述】:

我正在 mongo 中编写重复数据删除脚本,但它们返回的手机号码等于 null 或空字符串,认为这些都是重复的。我曾尝试在 mongo 中使用$ne,但无法使其正常工作。有谁知道如何返回手机号码不等于null 或空字符串的所有重复项?

    $mobile_duplicates = User::raw(function ($collection) {
        return $collection->aggregate(
            [
                [
                    '$limit' => 200000,
                ],
                [
                    '$group' => [
                        '_id' => [
                            'mobile', //=> '$mobile',
                        ],
                        'uniqueIds' => [
                            '$addToSet' => '$_id',
                        ],
                        'count' => [
                            '$sum' => 1,
                        ],
                    ],
                ],
                [
                    '$match' => [
                        // '_id' => [
                        //    '$ne' => "",
                        // ],
                        // '_id' => [
                        //    '$ne' => null,
                        // ],
                        'count' => [
                            '$gt' => 1,
                        ],
                    ],
                ]
            ],
            [
                'allowDiskUse' => true,
            ]
        );
    });

提前致谢!

【问题讨论】:

  • 标志,重复而不是回答重复,但是感谢您的指示,所以总是感谢 OP 试图关闭那里的问题。

标签: mongodb


【解决方案1】:

在这篇文章中找到了答案! stackoverflow.com/questions/14184099/...(将 $match 查询分成两个不同的查询 - 这对我有用:

$mobile_duplicates = User::raw(function ($collection) {
        return $collection->aggregate(
            [
                [
                    '$match' => [
                        'mobile' => [
                            '$ne' => '',
                            '$exists' => true,
                        ],
                    ],
                ],
                [
                    '$group' => [
                        '_id' => [
                            'mobile' => '$mobile',
                        ],
                        'uniqueIds' => [
                            '$addToSet' => '$_id',
                        ],
                        'count' => [
                            '$sum' => 1,
                        ],
                    ],
                ],
                [
                    '$match' => [
                        'count' => [
                            '$gt' => 1,
                        ],
                    ],
                ],
            ],
            [
                'allowDiskUse' => true,
            ]
        );
    });

【讨论】:

  • 请使用您问题上的编辑链接添加更多信息。 Post Answer 按钮应仅用于问题的完整答案。 - From Review
  • 这是完整的答案!我只是在等待时间限制后用绿色按钮标记它。
  • 只需从链接中详细说明您的答案。因为如果链接被删除,您的答案对其他人没有用
猜你喜欢
  • 1970-01-01
  • 2015-04-23
  • 2018-01-30
  • 2019-09-10
  • 2020-07-18
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 2017-04-01
相关资源
最近更新 更多