Skip to main content

Less than and greater than in MongoDB

$lt and $gt are the two commands used to get the values greater than given value and less than given value, it is same as between in sql server.

$lte and $gte will used as 'less than or equal to' and 'greater than or equal to'

Better to use $lt and $gt in the given find statement to get the exact records, because Mongodb allows users to pass the different data types for the same field, giving both $lt and $gt will give the actual and expected output.

Let the initial data in the database be:

{ "pname" : "Apple",  "Price" : 600,  "Location" : "US"  }
{ "pname" : "Microsoft",  "Price" : 400,  "Location" : "UK"  }
{ "pname" : "Motorolla",  "Price" : 250,  "Location" : "India"  }
{ "pname" : "lenevo",  "Price" : 230,  "Location" : "Argentina"  }
{ "pname" : "MI",  "Price" : 290,  "Location" : "Chile"  }
{ "pname" : "samsung",  "Price" : 320,  "Location" : "Singapore"  }


Using $lt and $gt for integer values

db.products.find({Price:{$gt:350}})

db.products.find({Price:{$lt:250}})

First statement will return the documents with Price greater than 350 and Second statement will return the documents of Price less than 250

Using $lte and $gte for string values

db.products.find({pname:{$gte:'h'}})

db.products.find({pname:{$lte:'D'}})

First statement will return the documents with pname ‘h’ and alphabetically greater than 'h'  and Second statement will return the documents of pname with ‘D’ and alphabetically less than 'D'

Using $lt and $gt together

db.products.find({Price:{$gt:290,$lt:400}})

The above Query will return the documents of price greater than 290 and less than 400

{ "pname" : "samsung", "Price" : 320, "Location" : "Singapore" }


Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.



    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete
  3. You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things
    App Development Course in Chennai
    Android Development Course in Chennai
    Android Training Institutes in Bangalore
    Android App Development Course in Bangalore
    Android Course in Coimbatore
    Android App Development Course in Coimbatore
    Android Course in Madurai

    ReplyDelete

Post a Comment