0
votes

I have a string, say "string", and a field in a collection, say "name".

I would like to find all the documents whose name is a substring of "string" (it would return for example documents whose name is "str").

I have looked on internet, and I only find the reciprocal, that is for example documents whose name is "string2".

Would someone know please ?

KR Zlotz

2
what is your MongoDB version ? - whoami - fakeFaceTrueSoul
4.2.8. Why do you ask ? - Zlotz

2 Answers

1
votes

There is no arbitrary substring match operator in MongDB that I know of, but you could try expressing your condition with $where or $regex.

0
votes

Recently I wanted to achieve the same in my project query below worked for me

db.find({name:{ $regex: new RegExp("^" + "your string pattern".toLowerCase(), "i") } }) 

Let me know if you find it useful.