Reputation: 1011
I have a MongoDB query as mentioned below:
Mailbox.find({ subject: new RegExp(query , 'i') });
This query gives me all the results which contains the query.For example my query is test then the output displays all the results which contains 'test'.
If i want results which have only starting letter 't' or word 'test' , how can it be done ?
Upvotes: 1
Views: 3051
Reputation: 151230
This is a general regex question and should be searchable
/^test/
The ^ caret signifies the start of the line.
Searching from the start is also advised in mongo for efficiency, meaning avoiding collection scans. More information on the forms of this, look at $regex in the documentation.
Keep this as a bookmark, and a reference for any other alterations you may need.
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
Upvotes: 5