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. Replies
    1. IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Online Training in India

      JavaScript Training in India

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. 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
  4. 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
  5. I know Laura well and she is principled, thoughtful, and extremely bright...more power to her!


    iso 27001 singapore

    ReplyDelete
  6. Hats off to your presence of mind..I really enjoyed reading your blog. I really appreciate your information which you shared with us.
    Project Management Apps

    ReplyDelete

Post a Comment