Create Collection
Begin with the creation of a new database collection in your database bank. The command attached below is creating a new collection called “Data” in our MongoDB database “test”. The response of { ok: 1 } indicates that the collection was successfully created and can be utilized further.
{ ok: 1 }
Insert Records to Collection
At the start of creating a collection, it must be empty as we have not added any documents to it yet. To perform the strcasecmp operator on a “Data” collection, it should have documents. Therefore, we will be using the MongoDB insertMany function to add numerous records into a collection named “Data” at once. The documents being inserted are in the format of key-value pairs, with keys “title” and “Desc” and their respective values. The values being inserted include “John”, “JOHN”, “iDk”, and “liSA” for the “title” key and “A random guy”, “GOOD BOY”, “IdONtkNoW”, and “ENginnerER” for the “Desc” key. The command is to add 4 new documents. The output shows the “acknowledged” as a boolean value, which is set to true, indicating that the query was acknowledged and executed by the MongoDB server successfully.
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63c90d6a42844a817c33167c"),
'1': ObjectId("63c90d6a42844a817c33167d"),
'2': ObjectId("63c90d6a42844a817c33167e"),
'3': ObjectId("63c90d6a42844a817c33167f")
}}
Display the Collection Records
After inserting a total of 4 records in the “Data” collection of the “test” database, we should verify the documents at least once. Therefore, we need to display the inserted records in the MongoDB shell for viewing. The command for viewing the records has been attached below. By using the MongoDB find() method, we retrieved all documents of the “Data” collection. The empty object {} passed as an argument to the find() method means that no filtering condition is applied and all documents in the collection will be returned. The output of this command is an array of documents, each one containing the _id, title, and Desc fields and their respective values, which were added earlier in the collection. The output confirms that the documents are present in the collection and can be retrieved for further processing.
[ {_id: ObjectId("63c90d6a42844a817c33167c"), title: 'John', Desc: 'A random guy' },
{ _id: ObjectId("63c90d6a42844a817c33167d"), title: 'JOHN', Desc: 'GOOD BOY' },
{_id: ObjectId("63c90d6a42844a817c33167e"), title: 'iDk', Desc: 'IdONtkNoW' },
{ _id: ObjectId("63c90d6a42844a817c33167f"), title: 'liSA', Desc: 'ENginnerER' } ]
Example # 01:
In our first illustration of MongoDB, we will be using the strcasecmp operator to perform a comparison between two string values: case-insensitive comparison. For this, we are going to run the query shown below in the MongoDB shell that is using MongoDB’s aggregate function to perform a series of operations on a collection named “Data”. The first operation is a $project stage, which modifies the documents in the collection by adding or removing fields. In this case, the $project stage is going to display all the records of the field “title” along with adding a new field named “Result” to each document, which is the result of comparing the value of the “title” field to the string “IDK” using the $strcasecmp operator.
The comparison is case-insensitive, meaning that “John” and “JOHN” would be considered equal. The new “Result” field displays the output of the comparison of the “title” field to “IDK”. 0 at the third record shows that the string “ID” is equal to the “iDK” string in the “title” field while the rest are 1.
[ {_id: ObjectId("63c90d6a42844a817c33167c"), title: 'John', Result: 1 },
{ _id: ObjectId("63c90d6a42844a817c33167d"), title: 'JOHN', Result: 1 },
{ _id: ObjectId("63c90d6a42844a817c33167e"), title: 'iDk', Result: 0 },
{ _id: ObjectId("63c90d6a42844a817c33167f"), title: 'liSA', Result: 1 } ]
Example # 02:
Let us have a new illustration of comparing strings in a case-insensitive manner within the MongoDB database. This time, the code we have been using is utilizing the same aggregate function to perform a comparison on the “title” field. The $project stage is creating a new field with the same name: “Result” that stores the output of the comparison. For comparison, we are using the same $strcasecmp operator. The $strcasecmp operator is passed with two arguments: “$title”, which is the value of the “title” field in each document, and “john”, which is the string being used as the comparison value. In this example, the first document has a “title” field of ‘John’, which is equal to “john”, so the Result is 0.
The second document has a “title” field of ‘JOHN’, which is also equal to “john” but in uppercase. So, the “Result” is also 0. The third document has a “title” field of ‘iDk’, which is less than “john” in terms of lexicographic order, so the result is -1. The fourth document has a “title” field of ‘liSA’, which is greater than “john” in terms of lexicographic order, so the Result is 1.
[ {_id: ObjectId("63c90d6a42844a817c33167c"), title: 'John', Result: 0 },
{ _id: ObjectId("63c90d6a42844a817c33167d"), title: 'JOHN', Result: 0 },
{ _id: ObjectId("63c90d6a42844a817c33167e"), title: 'iDk', Result: -1 },
{ _id: ObjectId("63c90d6a42844a817c33167f"), title: 'liSA', Result: 1 } ]
Example # 03:
In our last illustration, we will be using the “Desc” field of the “Data” collection to be compared with a new string. The $strcasecmp operator is being passed two arguments: “$Desc”, which is the value of the “Desc” field in each document and “idontknow”, which is the string being used as the comparison value.
The first, second, and 4th documents have a “Desc” field of ‘A random guy’, which is less than or greater than the string “idontknow” in terms of lexicographic order. So, the Result for these records is -1. The third document has a “Desc” field of ‘IdONtkNoW’, which is equal to “idontknow” regardless of the case of letters, so the Result is 0.
[ { _id: ObjectId("63c90d6a42844a817c33167c"), Desc: 'A random guy', Result: -1 },
{_id: ObjectId("63c90d6a42844a817c33167d"), Desc: 'GOOD BOY', Result: -1 },
{ _id: ObjectId("63c90d6a42844a817c33167e"), Desc: 'IdONtkNoW', Result: 0 },
{ _id: ObjectId("63c90d6a42844a817c33167f"), Desc: 'ENginnerER', Result: -1 } ]
Conclusion
This guide introduces the concept and use of the strcasecmp operator of MongoDB. We have created a new collection and performed aggregate() function commands along with the strcasecmp operator to demonstrate the use of the strcasecmp operator. The examples used in this article demonstrate how a single string can be compared case-insensitively to string values of collection fields.