25

I inserted the documents into a particular database in mongoDB. for example

 db.employee.insert({name:"nithin",age:22})
 db.employee.insert({name:"sreedevi",age:32})

now i wnat to retrive the documents whose name ending with character 'i'.

Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48
Nithin K Anil
  • 263
  • 1
  • 3
  • 6

1 Answers1

44

In Javascript shell, use $regex operator

db.employee.find({name: {$regex: "i$"}})
Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48
  • 2
    +1 You can add `find(...).pretty()` for a better arrangement. – Ionică Bizău Feb 28 '13 at 05:04
  • its easy using $regix db.employee.find({name:/i$/}) if i want to find the name starting with i use db.employee.find({name:/^i/}) – Nithin K Anil Feb 28 '13 at 06:04
  • With `^` you can have the advantage of your indexes, and thats why $regex is worth using in such cases. A suggestion you might want to consider, avoid adding features like "name ending with char" since they eat up unnecessary RAM and isn't scalable. – Sushant Gupta Feb 28 '13 at 08:57