skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. Using that example:

    [
      {
        "key": 1,
        "message": "😊"
      },
      {
        "key": 2,
        "message": "excellent service"
      }
    ]
    

    And that query:

    db.collection.find({
      message: {
        $in: [
          "😊"
        ]
      }
    })
    

    I returned the message you requested:

    [
      {
        "_id": ObjectId("5a934e000102030405000000"),
        "key": 1,
        "message": "😊"
      }
    ]
    

    Here you can see the query in Mongo playground: [Link to Mongo Playground]
    1

    Useful documentation:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search