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'.
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
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).
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.
In Javascript shell, use $regex operator
db.employee.find({name: {$regex: "i$"}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With