Pattern Matching With Regex
An equality filter cannot find "titles that start with In". A regular expression can. A regular expression (or regex) is a pattern for text. In mongosh you write it between two slashes, and you can use it as a filter value:
db.movies.find({ title: /^In/ }, { _id: 0, title: 1 })
Output:
[ { title: 'Inception' }, { title: 'Interstellar' } ]
^ means "the start of the text", so /^In/ matches titles that start with In. Without ^, the pattern can match anywhere in the text:
db.movies.find({ title: /st/ }, { _id: 0, title: 1 })
Output:
[ { title: 'Interstellar' }, { title: 'First Man' } ]
Both Interstellar and First Man have st somewhere inside their title.
Patterns match capital letters exactly. Add an i after the last slash to ignore case. $ means "the end of the text":
db.movies.find({ title: /LAND$/i }, { _id: 0, title: 1 })
Output:
[ { title: 'La La Land' } ]
/LAND$/i matches titles that end in "land", written in any case.
Use a regex when you need a pattern. When you know the exact value, a plain equality filter or $in is simpler.
In the exercise, find titles that contain "la" in any case.