Home > Back-end >  How to remove special characters from slug that already exists in MongoDB?
How to remove special characters from slug that already exists in MongoDB?

Time:09-13

My MongoDB Data looks like this:

{
  "_id": {
    "$oid": "630f3c32c1a580642a9ff4a0"
  },
  "title": "This is a title [Hello#$There1234] !!5678 @",
  "slug": "this--is-a-----title-[hello#$there1234]---!!5678-@----",
  "Post": "this is a post"
}

But I want it like this:

{
  "_id": {
    "$oid": "630f3c32c1a580642a9ff4a0"
  },
  "title": "This is a title [Hello#$There1234] !!5678 @",
  "slug": "this-is-a-title-hellothere1234-5678",
  "Post": "this is a post"
}

How to change slug for only - and alphabets and numbers, alphabet can be in any language.

CodePudding user response:

Here's one way that might work by deconstructing the current "slug" into "allowable" characters and then "$concat" the chars back into a single string. I have little regex-foo so I'm not sure this is the best "regex", but it may be sufficient. If your data is important, you should probably test this on some toy data first.

db.collection.update({},
[
  {
    "$set": {
      "slug": {
        "$rtrim": {
          "chars": "-",
          "input": {
            "$reduce": {
              "input": {
                "$regexFindAll": {
                  "input": "$slug",
                  "regex": "[\\p{Xan}-] "
                }
              },
              "initialValue": "",
              "in": {"$concat": ["$$value", "$$this.match"]}
            }
          }
        }
      }
    }
  }
],
{"multi": true}
)

Try it on mongoplayground.net.

UPDATE

This is a bit ugly, and demonstrates my lack of regex-foo, but by adding another stage to execute a javascript "$function", multiple - can be collapsed to just one. Hopefully a regex master will see this and simplify the whole thing.

db.collection.update({},
[
  {
    "$set": {
      "slug": {
        "$rtrim": {
          "chars": "-",
          "input": {
            "$reduce": {
              "input": {
                "$regexFindAll": {
                  "input": "$slug",
                  "regex": "[\\p{Xan}-] "
                }
              },
              "initialValue": "",
              "in": {"$concat": ["$$value", "$$this.match"]}
            }
          }
        }
      }
    }
  },
  {
    "$set": {
      "slug": {
        "$function": {
          "lang": "js",
          "args": ["$slug"],
          "body": "function(str) {return str.replace(RegExp('- ', 'g'), '-')}"
        }
      }
    }
  }
],
{
  "multi": true
})

Try it on mongoplayground.net.

  • Related