1

My documents looks like this.

{
   "sId": "s1",
   "source": "September Challenge %2s",
},
{
   "sId": "s2",
   "source": "September 2019 Challenge %2s"
},
{
   "sId": "s3",
   "source": "September Challenge 2019"
}

I was trying to find the documents by searching in source with regex 2. I am getting all the 3 documents. I want the result below.

{
   "sId": "s2",
   "source": "September 2019 Challenge %2s"
},
{
   "sId": "s3",
   "source": "September Challenge 2019"
}

first documents should be ignored because it has %2s, Second and third documents should come because it contains 2019. Can anyone please guide me how I can achieve the above.

abinas patra
  • 359
  • 3
  • 21

2 Answers2

1

Try the below regex.

db.getCollection('somecollection').find({field: {$regex: '(?<!%)<word>(?!=s)'}})
Zudhin
  • 266
  • 2
  • 5
0

From your comment it seems like you want to find documents with 2019 in the source field, even if it contains %2. You can do this with a more specific regex.

db.collection.find({ source: { $regex: "2019" } })
Montgomery Watts
  • 3,806
  • 2
  • 10
  • 24
  • Thanks :), but I am not specific to `2019`, it might be `2` or `0` or `9` or `september` anything. There are lot of documents contains `%2s`, I want to ignore documents which contains `%2s` but in case of second document, if i search with `2` or `september` it should not be ignored. – abinas patra Nov 27 '20 at 03:19