Chapter3 , Comparison operator

In the Chapter 3, Comparison Operator, after 2:20, the author says, $lte and $gte we lost few movies. I think $lte, $gte will yield more result than $gt, $lt…Could you please confirm? Thanks in advance

@Arivoli_48247

A short description

$lt - Lesser Than
$lte - Lesser Than Equals
$gt - Greater Than
$gte- Greater Than Equals

Example You have 5 records and naming the collection as “testing”

  1. { age : 10 }
  2. { age : 15 }
  3. { age : 20 }
  4. { age : 25 }
  5. { age : 30 }

Scenario 1: When we execute the following code for $lt

db.testing.find( { age : { $lt : 20 } )

This will return 2 records

  1. { age : 10 }
  2. { age : 15 }

Reason: Age is lesser than 20
Record 1: matching (10 < 20)
Record 2: matching (15 < 20)
Record 3: not matching (20 < 20)
Record 4: not matching ( 25 < 20 )
Record 5: not matching ( 30 < 20 )

Scenario 2: When we execute the following code for $lte

db.testing.find( { age : { $lte : 20 } )

This will return 3 records

  1. { age : 10 }
  2. { age : 15 }
  3. { age : 20 }

Reason: Age is lesser than or equals to 20
Record 1: matching (10 <= 20)
Record 2: matching (15 <= 20)
Record 3: matching (20 <= 20)
Record 4: not matching ( 25 <= 20 )
Record 5: not matching ( 30 <= 20 )

Scenario 3: When we execute the following code for $gt

db.testing.find( { age : { $gt : 20 } )

This will return 2 records

  1. { age : 25 }
  2. { age : 30 }

Reason: Age is greater than 20
Record 1: not matching (10 > 20)
Record 2: not matching (15 > 20)
Record 3: not matching (20 > 20)
Record 4: matching ( 25 > 20 )
Record 5: matching ( 30 > 20 )

Scenario 4: When we execute the following code for $gte

db.testing.find( { age : { $gte : 20 } )

This will return 3 records

  1. { age : 20 }
  2. { age : 25 }
  3. { age : 30 }

Reason: Age is greater than or equals to 20
Record 1: not matching (10 >= 20)
Record 2: not matching (15 >= 20)
Record 3: matching (20 >= 20)
Record 4: matching ( 25 >= 20 )
Record 5: matching ( 30 >= 20 )

You are right.
$lte, $gte will yield more results than $lt, $gt as the following queries confirm.
The runtimes that are right on the boundaries (90, 120) are shown in the 3rd query.

So in pseudo-code:
runtimes $lt 90 and $gt 120 + runtimes in [90, 120] = runtimes $lte 90 and $gte 120.

714 + 115 = 829
in the queries below.

MongoDB Enterprise Cluster0-shard-0:PRIMARY> 
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movieDetails.find({"runtime": {$gt: 90, $lt: 120}}).count()
714
MongoDB Enterprise Cluster0-shard-0:PRIMARY> 
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movieDetails.find({"runtime": {$gte: 90, $lte: 120}}).count()
829
MongoDB Enterprise Cluster0-shard-0:PRIMARY> 
MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.movieDetails.find({"runtime": {$in: [90, 120]}}).count()
115
MongoDB Enterprise Cluster0-shard-0:PRIMARY> 

But the instructor is talking about comparing these two queries:

db.movieDetails.find({runtime: {$gte: 90, $lte: 120}}, {_id: 0, title: 1, runtime: 1})
db.movieDetails.find({runtime: {$gt: 90}}, {_id: 0, title: 1, runtime: 1})

The first query above is at timestamp 2:05 and again at timestamp 2:23 in the video.
The second query above is at timestamp 2:18 in the video. (You have to pause the video very quickly to see it though, the query is seen only for a fraction of a second!)

1 Like

Thank you very much for all who explained it in detail…Thanks