Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the documents whose values ending with a particular character in mongoDB

Tags:

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'.

like image 267
Nithin K Anil Avatar asked Feb 28 '13 04:02

Nithin K Anil


People also ask

How do I capture a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

How do I get the last element in MongoDB?

To find last object in collection, at first sort() to sort the values. Use limit() to get number of values i.e. if you want only the last object, then use limit(1).

How do I find a specific data in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.


1 Answers

In Javascript shell, use $regex operator

db.employee.find({name: {$regex: "i$"}})
like image 137
Sushant Gupta Avatar answered Oct 07 '22 12:10

Sushant Gupta