
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to retrieve the documents whose values end with a particular character in MongoDB?
Following is the syntax to retrieve the documents whose values end with a particular character in MongoDB
db.yourCollectionName.find({yourFieldName: {$regex: "yourEndingCharacter$"}}).pretty();
Let us first create a collection with documents
>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Adam","StudentAge":25,"StudentCountryName":"LAOS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45b32d66697741252456") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Sam","StudentAge":24,"StudentCountryName":"ANGOLA"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45c02d66697741252457") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Robert","StudentAge":21,"StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45cb2d66697741252458") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Chris","StudentAge":20,"StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45d92d66697741252459") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Larry","StudentAge":23,"StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45eb2d6669774125245a") }
Following is the query to display all documents from a collection with the help of find() method
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9c45b32d66697741252456"), "StudentName" : "Adam", "StudentAge" : 25, "StudentCountryName" : "LAOS" } { "_id" : ObjectId("5c9c45c02d66697741252457"), "StudentName" : "Sam", "StudentAge" : 24, "StudentCountryName" : "ANGOLA" } { "_id" : ObjectId("5c9c45cb2d66697741252458"), "StudentName" : "Robert", "StudentAge" : 21, "StudentCountryName" : "AUS" } { "_id" : ObjectId("5c9c45d92d66697741252459"), "StudentName" : "Chris", "StudentAge" : 20, "StudentCountryName" : "UK" } { "_id" : ObjectId("5c9c45eb2d6669774125245a"), "StudentName" : "Larry", "StudentAge" : 23, "StudentCountryName" : "US" }
Following is the query to retrieve the documents whose values end with a particular character in MongoDB. We are checking for values ending with character “S”
> db.retrieveDocumentsWithEndsWithParticularCharacterDemo.find({StudentCountryName: {$regex: "S$"}}).pretty();
This will produce the following output
{ "_id" : ObjectId("5c9c45b32d66697741252456"), "StudentName" : "Adam", "StudentAge" : 25, "StudentCountryName" : "LAOS" } { "_id" : ObjectId("5c9c45cb2d66697741252458"), "StudentName" : "Robert", "StudentAge" : 21, "StudentCountryName" : "AUS" } { "_id" : ObjectId("5c9c45eb2d6669774125245a"), "StudentName" : "Larry", "StudentAge" : 23, "StudentCountryName" : "US" }
- Related Questions & Answers
- How to retrieve documents from a collection in MongoDB?
- How to retrieve all the documents from a MongoDB collection using Java?
- Add a character in the end to column values with MySQL SELECT?
- How to select documents with values above the average in MongoDB?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Ignore null values in MongoDB documents
- Can I retrieve multiple documents from MongoDB by id?
- Update values in multiple documents with multi parameter in MongoDB?
- How to find MongoDB documents with a specific string?
- How to sort a particular value at the end in MySQL?
- Evaluate one of more values from a MongoDB collection with documents
- Match MongoDB documents with fields not containing values in array?
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to find matching documents given an array with values?
- Find documents which contain a particular value in MongoDB using Regular Expressions?
Advertisements