Whether the last four digits of the ID card are unique

As we all know, ID cards are unique.

But someone suddenly asked, are the last four digits of the ID card in our database unique? Can you query the database?

My first reaction to this question is, of course, it's not the only one.

non-rigorous interpretation

On the premise of not performing database verification, we just think about it logically. The ID card has a total of 18 digits, and the last four digits are the four digits after the birthday. Then no matter how you arrange it, it is at most 10 10 10 * 11 Combinations are sure to repeat.

Rigorous explanation

Let's understand it logically

Arrangement rules of ID card:
ID card number is composed of 18 digits
. The first and second digits indicate: the code of the province where it is located.
The third and fourth digits indicate: the code of the city where it is located.
The fifth and sixth digits indicate: the district and county where it is located.
The 7th to 14th digits of the code indicate: year, month, and day of birth (the 7th, 8th, 9th, and 10th digits are the year, the 11th and 12th digits are the month, and the 13th and 14th digits are the day)
. The 15th to 17th digits are all The sequence code of people born in the same address jurisdiction and the same month and day in the same year, and the 17th digit also has the function of gender identification. The 18th digit of men’s singles and women’s doubles is a check
code: it can be a number from 0 to 9, and sometimes it is also used X means

Therefore, in different provinces and municipalities, the last four digits of the ID card can be repeated.

database validation

Yes, we are programmers, and we can use data to convince others when theory fails.
Here my database is a mongo database, so my query statement is as follows

db.user.aggregate([
    {
        $match: {
            //请原谅数据库中那些不规范的身份证号
            card_id: {
                $exists: true,
                $nin: ["", null]
            }
        }
    },
    {
        $project: {
            "_id": 1,
            "name": 1,
            "card_id": 1,
            "card_id_info": {
                $substr: ["$card_id", 14, 4]
            }
        }
    },
    //请原谅数据库中那些不规范的身份证号
    {
        $match: {
            card_id_info: {
                $nin: ["", null]
            }
        }
    },
    {
        $group: {
            _id: "$card_id_info",
            count: {
                $sum: 1
            },
						//增加输出
            uniqueIds: {
                "$addToSet": {
                    _id: "$_id",
                    name: "$name",
                    card_id: "$card_id",
                    card_id_info: "$card_id_info"
                }
            }
        }
    },
    {
		//让我们看看重复的多的是哪些
        $sort: {
            count:  - 1
        }
    }
])

message

data ruthless but real

Guess you like

Origin blog.csdn.net/Long861774/article/details/127475038