Search code examples
mongodbemoji

How to query a mongoDB object to find if it contain any emoji


I have a collection that records comments from users. Something like the below:

{
    _id:1
    "statusReason" : {
                       "text" : {
                                   "value" :"Hello there 😊"
                                }
                     }

}

I need to write a query that can identify any comments containing any emoji.

MongoDB version: 3.4.24


Solution

  • As MongoDB stores by default using UTF-8, you can simply search with regex of the unicode range of the emojis that you are interested of. You can check for the latest unicode range for emojis here

    db.collection.find({
      "statusReason.text.value": {
        "$regex": "[\\x{2194}-\\x{1FAF8}]"
      }
    })
    

    Here is the Mongo playground for your reference.