G M
G M

Reputation: 22449

Ignore substring of characters between text records words when querying MongoDB databases

I have a MongoDB database with some special encoding:

[
  {
    "key": "should be matched"
  },
  {
    "key": "should [[be matched]]"
  },
  {
    "key": "also this should be matched [[please]]"
  }
]

I would like to ignore the double square brackets when looking for a sentence:

db.collection.find({ 
  key: "should be matched"
})

Should be modified so that returns both results. The square brackets could be everywhere in the sentence and this is not known a priori. I have lost many hours and I came up with this solution:

$where: "this.key.replace(/\[\[|\]\]/g,'').includes('should be matched')"

Unfortunatly, seems that where operator can not be used maybe for restriction and by the way is not efficient. Is there any easy fix for this problem?

Playground here.

Upvotes: 0

Views: 73

Answers (1)

ray
ray

Reputation: 15217

You can use $replaceAll in and aggregation pipeline to sanitize the field(i.e. remove [ and ] characters and perform your $match afterwards.

db.collection.aggregate([
  {
    "$addFields": {
      "sanitizedKey": {
        "$replaceAll": {
          "input": {
            "$replaceAll": {
              "input": "$key",
              "find": "[",
              "replacement": ""
            }
          },
          "find": "]",
          "replacement": ""
        }
      }
    }
  },
  {
    $match: {
      sanitizedKey: "should be matched"
    }
  },
  {
    "$project": {
      sanitizedKey: false
    }
  }
])

Here is the Mongo playground for your reference.

Upvotes: 1

Related Questions