Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Tuesday 12 March 2024

mask mobile numbers in MongoDB

In MongoDB, you can mask mobile numbers using the aggregation framework along with string manipulation operators. Below is an example of how to implement this:


db.collection.aggregate([

  {

    $addFields: {

      masked_mobile_number: {

        $switch: {

          branches: [

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 10] }, then: {$concat: ["XXX-XXX-", {$substr: ["$mobile_number", 7, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 11] }, then: {$concat: ["XX-XXX-", {$substr: ["$mobile_number", 8, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 12] }, then: {$concat: ["X-XXX-", {$substr: ["$mobile_number", 9, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 13] }, then: {$concat: ["XXX-XXXX-", {$substr: ["$mobile_number", 10, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 14] }, then: {$concat: ["XX-XXXX-", {$substr: ["$mobile_number", 11, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 15] }, then: {$concat: ["X-XXXX-", {$substr: ["$mobile_number", 12, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 16] }, then: {$concat: ["XXXX-XXXX-XXXX-", {$substr: ["$mobile_number", 12, -1]}]} },

          ],

          default: "Invalid Mobile Number"

        }

      }

    }

  }

])


This pipeline adds a new field masked_mobile_number to each document in the collection, which contains the masked mobile number based on its length.


Example:


Let's say you have a document in the collection with the mobile number '1234567890':


db.collection.insertOne({mobile_number: '1234567890'})


Applying the aggregation pipeline:


db.collection.aggregate([

  {

    $addFields: {

      masked_mobile_number: {

        $switch: {

          branches: [

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 10] }, then: {$concat: ["XXX-XXX-", {$substr: ["$mobile_number", 7, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 11] }, then: {$concat: ["XX-XXX-", {$substr: ["$mobile_number", 8, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 12] }, then: {$concat: ["X-XXX-", {$substr: ["$mobile_number", 9, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 13] }, then: {$concat: ["XXX-XXXX-", {$substr: ["$mobile_number", 10, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 14] }, then: {$concat: ["XX-XXXX-", {$substr: ["$mobile_number", 11, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 15] }, then: {$concat: ["X-XXXX-", {$substr: ["$mobile_number", 12, -1]}]} },

            { case: { $eq: [{$strLenCP: "$mobile_number"}, 16] }, then: {$concat: ["XXXX-XXXX-XXXX-", {$substr: ["$mobile_number", 12, -1]}]} },

          ],

          default: "Invalid Mobile Number"

        }

      }

    }

  }

])


The result would be:

{

  "mobile_number": "1234567890",

  "masked_mobile_number": "XXX-XXX-7890"

}


Here are few FAQs:-


1. Why should mobile numbers be masked in a database?

   - Mobile numbers can contain sensitive information, and masking them helps protect user privacy and prevents unauthorized access.


2. Can the original mobile number be retrieved from the masked version?

   - Ideally, the masking process should be irreversible to maintain data security. Therefore, the original mobile number should not be easily retrievable from the masked version.


3. How can I ensure that masked mobile numbers remain usable for certain operations?

   - By retaining some part of the original mobile number, such as the last few digits, you can maintain the usability of the data for operations like identification or verification while still protecting sensitive information.


4. Are there any legal requirements for masking mobile numbers?

   - Depending on the jurisdiction and the nature of the data being handled, there may be legal requirements or industry standards mandating the protection of personally identifiable information (PII), which includes mobile numbers.


5. Can I customize the masking format for mobile numbers?

   - Yes, you can customize the

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.