The Wayback Machine - https://web.archive.org/web/20240927201637/https://www.geeksforgeeks.org/mongodb-strcasecmp-operator/
Open In App

MongoDB $strcasecmp Operator

Last Updated : 27 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

$strcasecmp operator in MongoDB is a string expression operator used to perform case-insensitive comparisons between two strings. It is commonly utilized within the aggregation pipeline to facilitate these comparisons.

In this article, We will learn about the MongoDB $strcasecmp Operator by understanding various examples.

MongoDB $strcasecmp Operator

The $strcasecmp operator in MongoDB is used to compare two strings. It is a string expression operator used in the aggregation pipeline to perform case-insensitive string comparisons.

The result of this operator can be explained in three cases:

  • If the first string is greater than the second string, then this operator will return 1. 
  • If the first string is less than the second string, then this operator will return -1. 
  • If both the strings are equal, then this operator will return 0.

Syntax

{ $strcasecmp: [ <expression1>, <expression2> ] }

Here, the arguments passed in this operator can be any valid expression until they resolve to strings.

Examples of MongoDB $strcasecmp Operator

To understand it better, let’s look at some examples of the MongoDB $strcasecmp Operator.

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: employee
Document: three documents that contain the details of the employees in the form of field-value pairs.

Output

demo database and collection

Example 1: Using $strcasecmp Operator

In this example, we are going compare the value of the department field of all the documents present in employee collection with the “development” string using $strcasecmp operator. 

Query:

db.employee.aggregate([
... {$project: {"name.first": 1, _id: 0, result:
... {$strcasecmp: ["$department", "development"]}}}])

Output:

Example 2: Using $strcasecmp Operator in the Embedded Document

In this example, we are going compare the value of the name.first field of all the documents present in employee collection with the “Sunita” string using $strcasecmp operator. 

Query:

db.employee.aggregate([
... {$project: {result: {$strcasecmp: ["$name.first", "Sunita"]}}}])

Output:

using $strcasecmp operator in the embedded document example output

KeyTakeAways About MongoDB strcasecmp Operator

  • The MongoDB $strcasecmp operator is used in the aggregation pipeline stages to perform a case-insensitive comparison of two strings.
  • It returns 1 if the first string is “greater than” the second string, 0 if the two strings are equal, and -1 if the first string is “less than” the second string.
  • The operator is useful for scenarios where case sensitivity is not required, and a simple string comparison is needed.
  • $strcasecmp internally capitalizes strings before comparing them to provide a case-insensitive comparison.
  • It may not make sense when applied to glyphs outside the Roman alphabet.

Conclusion

The MongoDB $strcasecmp operator is a valuable tool for performing case-insensitive string comparisons within the aggregation pipeline. By returning 1, -1, or 0 based on the comparison results, it allows for flexible and efficient string comparisons. Whether comparing field values in simple or embedded documents, the $strcasecmp operator helps developers handle case-insensitive data scenarios effectively.

FAQs on MongoDB $strcasecmp

What is the purpose of the $strcasecmp operator in MongoDB?

The $strcasecmp operator is used to perform case-insensitive comparisons between two strings in MongoDB, typically within the aggregation pipeline.

What does the $strcasecmp operator return when the two strings are equal?

When the two strings are equal, the $strcasecmp operator returns 0.

Can the $strcasecmp operator be used with embedded documents?

Yes, the $strcasecmp operator can be used to compare strings within embedded documents in MongoDB.

Is the $strcasecmp operator applicable to non-Roman alphabet glyphs?

The $strcasecmp operator may not make sense when applied to glyphs outside the Roman alphabet, as it internally capitalizes strings before comparing them to provide a case-insensitive comparison


Previous Article
Next Article

Similar Reads

How to Integrate MongoDB Atlas and Segment using MongoDB Stitch
Data integration has become a crucial component of modern business strategies, allowing companies to enhance the power of data for informed decision-making. According to a recent survey, 85% of businesses believe that integrating data across multiple platforms is important for their success. In this article, We will learn about What is MongoDB Stit
5 min read
Connect MongoDB Atlas Cluster to MongoDB Compass
MongoDB Compass is a free GUI for MongoDB. You might want to connect MongoDB Atlas Cluster to MongoDB Compass to take benefit of the GUI model for database administration. This guide explains how to connect MongoDB Atlas Cluster to MongoDB Compass. The connection process involves obtaining a connection string from MongoDB Atlas, configuring MongoDB
4 min read
MongoDB Shell vs MongoDB Node.JS Driver
MongoDB Shell and the MongoDB Node.js Driver are both tools for interacting with MongoDB, but they serve different purposes and are used in different contexts. Here's a comparison of the two: MongoDB Node.JS DriverA Node.js package called the MongoDB Node.js Driver enables programmatic communication between applications and MongoDB databases. Throu
4 min read
MongoDB Compass vs MongoDB Atlas
MongoDB is a popular NoSQL database known for flexibility and scalability. MongoDB Compass offers a graphical interface for managing databases visually while MongoDB Atlas is a cloud-based service for automated management and scaling of MongoDB databases. In this article, We will learn about the in-depth understanding of the difference between the
5 min read
MongoDB - Current Date Operator ($currentDate)
MongoDB provides different types of field update operators to update the values of the fields of the documents and $currentDate operator is one of them. This operator is used to set the value of a field to the current date (either as a timestamp or as a Date). The default type of $currentDate operator is a Date. This operator can also work with emb
2 min read
MongoDB $ln Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and $ln operator is one of them. This operator is used to find the natural logarithm of a number and returns the result as a double. Syntax: { $ln: <number> } Here, the number is a valid expression until it resolves to a non- n
1 min read
MongoDB $sqrt Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages $sqrt operator is one of them. This operator is used to find the square root of a positive number and returns the result as a double. Syntax: { $sqrt: <number> } Here, the number is a valid expression until it resolves to a non
2 min read
MongoDB $mod Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and $mod operator is one of them. This operator is used to divide one number by another number and return the remainder. Syntax: { $mod: [ <expression1>, <expression2> ] } Here, in this operator, the arguments passed in a
1 min read
How to use the $lookup operator in aggregation pipelines in MongoDB?
One of the essential stages of the Aggregation Pipeline is the $lookup. It enables us to accomplish a left outer join between two collections. This step is quite beneficial when we have to compose data from different collections into a single document. The Aggregation Pipeline in MongoDB is a robust tool that allows us to manipulate and analyze dat
4 min read
MongoDB - $concat Operator
The $concat operator in MongoDB is a useful tool for string manipulation within aggregation pipelines. It allows us to concatenate strings or expressions to create new fields or modify existing ones. In this article, We will learn about the $concat operator in MongoDB by understanding various examples in detail. MongoDB - $concat OperatorThe $conca
3 min read
MongoDB $subtract Operator
MongoDB $subtract operator is a powerful tool in the aggregation pipeline that allows users to perform subtraction operations on numbers or dates. In this article, We will learn about MongoDB $subtract Operator by understanding various examples in detail. MongoDB $subtract OperatorMongoDB provides different types of arithmetic expression operators
3 min read
MongoDB $addToSet Operator
The $addToSet operator in MongoDB allows us to add elements to an array field if they are not already present and ensure that the array contains unique values. In this article, We will learn about the MongoDB $addToSet Operator by understanding various examples in detail. $addToSet Operator in MongoDBThe $addToSet operator in MongoDB is used to add
3 min read
MongoDB - Rename Operator ($rename)
MongoDB $rename operator is a powerful tool for updating field names within documents which ensures data consistency and structure. This operator allows developers to rename fields in a MongoDB collection, including those nested within documents or arrays. In this article, We will learn about the MongoDB $rename by understanding various examples in
4 min read
MongoDB $arrayElemAt Operator
MongoDB $arrayElemAt operator is a key component of the aggregation framework, allowing users to retrieve specific elements from arrays within their documents. In this article, We will learn about the MongoDB $arrayElemAt by understanding various examples in detail and so on. MongoDB $arrayElemAtMongoDB provides different types of array expression
3 min read
MongoDB - $setOnInsert Operator
The $setOnInsert operator in MongoDB is a powerful tool used in updating operations with the upsert option. It allows us to specify values that should be set only when a new document is inserted. In this article, we will learn about the $setOnInsert Operator in MongoDB in detail and so on. MongoDB $setOnInsert OperatorMongoDB $setOnInsert Operator
4 min read
MongoDB - Positional Operator ($)
MongoDB provides a variety of array update operators to modify the values within array fields in documents. One such operator is the positional operator $. This operator allows us to update an element in an array without explicitly specifying the position of that element. In this article, We will learn about the Positional Operator($) in MongoDB in
4 min read
MongoDB $multiply Operator
In MongoDB, the $multiply operator is a powerful tool used in aggregation pipelines to perform multiplication operations. This operator takes one or more expressions as arguments and multiplies them to produce a result. In this article, We will learn about the MongoDB $multiply Operator in detail. MongoDB $multiply OperatorMongoDB provides differen
3 min read
MongoDB $push Operator
The $push operator in MongoDB is a powerful tool used to update arrays within documents. It appends the entire array as a single element. To add each element of the array individually, the $each modifier can be used alongside $push. In this article, We will learn about the MongoDB $push Operator by understanding various examples in detail. $push Op
5 min read
MongoDB $size Operator
MongoDB $size operator is an important tool for calculating the total number of elements within an array. This operator plays a crucial role in MongoDB's aggregation pipeline stages, particularly for tasks requiring the analysis and manipulation of array data. In this article, We will learn about the MongoDB $size Operator by understanding various
3 min read
MongoDB $abs operator
The $abs operator in MongoDB is a fundamental arithmetic expression operator used in aggregation pipeline stages. Its primary function is to calculate the absolute value of a specified number. In this article, We will learn about the MongoDB $abs operator by understanding various examples in detail. MongoDB $abs operatorMongoDB provides different t
3 min read
MongoDB $add Operator
The $add operator in MongoDB is a versatile arithmetic expression operator used to add numbers together or concatenate numbers and dates in the aggregation pipeline. This operator plays an important role in data manipulation and transformation. In this article, We will learn about the MongoDB $add Operator by understanding various examples in detai
3 min read
MongoDB $divide Operator
In MongoDB, the $divide operator is a powerful tool used to perform division between two numerical values. It allows for precise arithmetic operations directly within the database queries, enhancing the capability to manipulate and analyze data. In this article, We will learn about the MongoDB $divide Operator by understanding various examples in d
3 min read
MongoDB $toUpper Operator
The MongoDB $toUpper operator is a powerful tool used to convert string values to uppercase within the aggregation framework. It is particularly useful when working with text data that requires case normalization. In this article, We will learn about the MongoDB $toUpper Operator by understanding various examples in detail. MongoDB $toUpper Operato
3 min read
MongoDB - $pullAll Operator
MongoDB $pullAll operator is a crucial tool for efficiently managing arrays within MongoDB documents. It allows users to remove all instances of specified values from an array and provides a direct way to update array fields. In this article, We will learn about the MongoDB $pullAll Operator by understanding various examples in detail. MongoDB $pul
3 min read
MongoDB $cmp Operator
The MongoDB $cmp operator is a powerful tool for comparing two values within documents, commonly used in aggregation pipelines for sorting or conditional operations. In this article, We will learn about the MongoDB $cmp Operator by understanding various examples in detail. MongoDB $cmp OperatorThe MongoDB $cmp operator is used to compare two values
3 min read
MongoDB $reverseArray Operator
The MongoDB $reverseArray operator is a valuable tool for reversing the order of elements in an array, commonly used for various processing or presentation purposes. In this article, We will learn about the MongoDB $reverseArray Operator by understanding various examples in detail. MongoDB $reverseArray OperatorMongoDB $reverseArray Operator is use
4 min read
MongoDB - $isArray Operator
The MongoDB $isArray operator serves as a crucial tool within the aggregation pipeline and enables the verification of whether a specified expression represents an array. In this article, We will learn about the MongoDB $isArray Operator by understanding various examples in detail. MongoDB $isArray OperatorMongoDB $isArray Operator is an array expr
3 min read
MongoDB $ceil Operator
In MongoDB, the $ceil operator is a powerful tool used in aggregation pipelines to round numbers up to the nearest integer greater than or equal to the original number. In this article, We will learn about the MongoDB $ceil Operator in detail. MongoDB $ceil OperatorMongoDB $ceil operator is used in aggregation pipelines to perform mathematical roun
3 min read
MongoDB $floor Operator
In MongoDB, the $floor operator is a powerful tool in aggregation pipelines that allow for the rounding down of numbers to the nearest integer less than or equal to the original number. In this article, We will learn about the MongoDB $floor Operator in detail by understanding various examples and so on. MongoDB $floor OperatorMongoDB $floor operat
3 min read
MongoDB $exp Operator
MongoDB's aggregation framework provides a powerful set of tools for data manipulation and processing. One such tool is the $exp operator which allows users to perform exponential calculations within aggregation pipelines. In this article, We will learn about the MongoDB $exp Operator in detail. MongoDB $exp OperatorThe $exp operator in MongoDB is
3 min read