
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Implementing String Comparison in MongoDB?
To implement string comparison in MongoDB, use $strcasecmp. It performs case-insensitive comparison of two strings. It returns −
1 if first string is “greater than” the second string.
0 if the two strings are equal.
-1 if the first string is “less than” the second string.
Let us create a collection with documents −
> db.demo490.insertOne({"Name1":"John","Name2":"john"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8496ccb0f3fa88e22790bb") } > db.demo490.insertOne({"Name1":"David","Name2":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8496d9b0f3fa88e22790bc") } > db.demo490.insertOne({"Name1":"Carol","Name2":"Carol"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8496e5b0f3fa88e22790bd") }
Display all documents from a collection with the help of find() method −
> db.demo490.find();
This will produce the following output −
{ "_id" : ObjectId("5e8496ccb0f3fa88e22790bb"), "Name1" : "John", "Name2" : "john" } { "_id" : ObjectId("5e8496d9b0f3fa88e22790bc"), "Name1" : "David", "Name2" : "Bob" } { "_id" : ObjectId("5e8496e5b0f3fa88e22790bd"), "Name1" : "Carol", "Name2" : "Carol" }
Following is the query to implement string comparison in MongoDB −
> db.demo490.aggregate( ... [ ... { ... $project: ... { ... Name1: 1, ... Name2: 1, ... Result: { $strcasecmp: [ "$Name1", "$Name2" ] } ... } ... } ... ] ... )
This will result the following output −
{ "_id" : ObjectId("5e8496ccb0f3fa88e22790bb"), "Name1" : "John", "Name2" : "john", "Result" : 0 } { "_id" : ObjectId("5e8496d9b0f3fa88e22790bc"), "Name1" : "David", "Name2" : "Bob", "Result" : 1 } { "_id" : ObjectId("5e8496e5b0f3fa88e22790bd"), "Name1" : "Carol", "Name2" : "Carol", "Result" : 0 }
- Related Articles
- Implementing MongoDB map-reduce
- String Comparison in Java
- Implementing MongoDB $exists and $ne?
- Bash String Comparison
- Java String Comparison Methods.
- Case sensitive string comparison in Java.
- Case-insensitive string comparison in C++
- How to perform string comparison in TypeScript?
- Perform element-wise comparison of two string arrays using a comparison operator in Numpy
- Java string comparison sample code examples
- How to do case-sensitive string comparison in JavaScript?
- How to make SQL case sensitive string comparison in MySQL?
- How to use comparison operator for numeric string in MySQL?
- How MySQL can perform case-sensitive string comparison?
- Java String comparison, differences between ==, equals, matches, compareTo().

Advertisements