查找非空mongodb的重复项

时间:2015-12-09 16:20:17

标签: mongodb

我在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,
            ]
        );
    });

提前致谢!

1 个答案:

答案 0 :(得分:0)

在这篇文章中找到了答案! 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,
            ]
        );
    });