String Comparison: MongoDB


Today we shall see how we can compare strings using comparison operators like,
$ne – not equal to
$gt – greater than
$gte – greater than or equal to
$lt – less than
$lte – less than or equal to

(In our video we have clearly illustrated the use of $lt and $gt. Your task is to understand it and try other comparison operators, and you can share your results in the comment section below.)

red apple green apple Comparison Operators: MongoDB

Related Read: Comparison Operators: MongoDB

Documents in our collection
test database, names collection.

1
2
3
4
5
6
7
8
9
10
11
 
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53be5d8604cc1cb0a7bfc3c5"), "name" : "Farhan" }
{ "_id" : ObjectId("53be5d9204cc1cb0a7bfc3c6"), "name" : "Gangs" }
{ "_id" : ObjectId("53be5d9904cc1cb0a7bfc3c7"), "name" : "Hum" }
{ "_id" : ObjectId("53be5e3704cc1cb0a7bfc3c8"), "name" : 25 }

Here we have names in alphabetical order, and an odd name with numeric value 25.

We’ll query the collection using $lt operator.

1
2
3
4
 
> db.names.find({"name": {$lt: "C"}});
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }

It prints all strings which are lexicographically less than the capital letter “C”.

We’ll query the collection using $lt and $gt operator.

1
2
3
4
5
 
> db.names.find({"name": {$lt: "F", $gt: "C"}});
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }

This outputs strings which are greater than character “C” and less than character “F”.

Here we check for names/strings which are less than character “C” as well as greater than character “C”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
> db.names.find({"name": {$lt: "C"}});
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
 
 
> db.names.find({"name": {$gt: "C"}});
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53be5d8604cc1cb0a7bfc3c5"), "name" : "Farhan" }
{ "_id" : ObjectId("53be5d9204cc1cb0a7bfc3c6"), "name" : "Gangs" }
{ "_id" : ObjectId("53be5d9904cc1cb0a7bfc3c7"), "name" : "Hum" }
>

But in both cases the entry with name as 25 doesn’t appear. This concludes that, in MongoDB while we compare string/character using comparison operator, the comparison occurs only between strings and character and not with other datatypes.

Comparison Operators on Strings: MongoDB


[youtube https://www.youtube.com/watch?v=Ts2canvhQCY]

YouTube Link: https://www.youtube.com/watch?v=Ts2canvhQCY [Watch the Video In Full Screen.]



Note:
Lexicographical order: In mathematics, the lexicographic or lexicographical order is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters.

Leave a Reply

Your email address will not be published. Required fields are marked *