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"
}

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements