Reputation: 599
I am worked on my current project with MONGO, I need a query if i am search a letter need to search every word
ex: I have 3 topics named in my db as below
chemistry, chemical, organic chemistry
i tried with query like below
db.topics.find({name: { '$regex': '^che', '$options': 'i' }}, {})
but i am getting results of chemistry and chemical only
but my requirement is if i search with che then that letter need to check firtst letters of every word and results comes with chemistry, chemical and organic chemistry also (because in organic chemistry have word starts with chemistry
Upvotes: 2
Views: 14281
Reputation: 1068
To search letters throw every word, you need to do something like db.topics.find({name: '/che/'})
For more detailed answer, please check this link
Upvotes: 1
Reputation: 505
db.topics.find({name: { '$regex': 'che', '$options': 'i' }}, {})
^ in regex means "start" ^che
are text with start with che
and if u want che was first in any word u must match spaces:
db.topics.find({name: { '$regex': '(\s+che|^che)', '$options': 'i' }}, {})
for better understand regex check this : https://www.regextester.com/
Upvotes: 6