MongoDB: Use Greater Than & Less Than In Queries

Hello friends, In this article, we will perform these queries in MongoDB and create APIs in Dot Net Core. example Less than, Less than or equal, Greater than, and Greater than or equal.

You can use the following queries in MongoDB to perform greater than or less than queries:

  • $gt: Greater than
  • $lt: Less than
  • $lte: Less than or equal
  • $gte: Greater than or equal

Prerequisites:

  • Basic knowledge of .NET Core and MongoDB.
  • Code editors like Visual Studio 2019

Example:

1: Greater Than Query

db.StudentAge.find({Age: {$gt:19}})

2: Less Than Query

db.StudentAge.find({Age: {$lt:19}})

3: Less than or equal

db.StudentAge.find({Age: {$lte:19}})

4: Greater less than or equal

db.StudentAge.find({Age: {$gte:19}})

5: Greater Than and Less Than Query

db.StudentAge.find({Age: {$gt:19,$lt:22}})

6: Greater Than or Less Than Query

db.StudentAge.find({ "$or": [ {"Age": {$gte: 19}}, {"Age": {$lte: 20}} ] })

Dot Net Example:

using System;
using DotNetWithMongo.Models;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;

namespace DotNetWithMongo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentAgeController : ControllerBase
    {
        private readonly IMongoCollection<StudentAgeInfo> _studentsAge;
        public StudentAgeController(IMongoDBSettings settings)
        {
            var client = new MongoClient(settings.ConnectionURI);
            var database = client.GetDatabase(settings.DatabaseName);

            _studentsAge = database.GetCollection<StudentAgeInfo>(settings.StudentAge);
        }

        [HttpGet]
        [Route("Greaterthan")]
        public IActionResult Greaterthan(int age)
        {
            try
            {
                var filter = new BsonDocument
                {{ "Age", new BsonDocument{{ "$gt", age }}}};

                var data = _studentsAge.Find(filter).ToList();

                return Ok(data);
            }
            catch (Exception ex)
            {
                return Ok(ex.Message);
            }
        }

        [HttpGet]
        [Route("LessThan")]
        public IActionResult LessThan(int age)
        {
            try
            {
                var filter = new BsonDocument
                {{ "Age", new BsonDocument{{ "$lt", age }}}};

                var data = _studentsAge.Find(filter).ToList();

                return Ok(data);
            }
            catch (Exception ex)
            {
                return Ok(ex.Message);
            }
        }

        [HttpGet]
        [Route("LessThanOrEqual")]
        public IActionResult LessThanOrEqual(int age)
        {
            try
            {
                var filter = new BsonDocument
                {{ "Age", new BsonDocument{{ "$lte", age }}}};

                var data = _studentsAge.Find(filter).ToList();

                return Ok(data);
            }
            catch (Exception ex)
            {
                return Ok(ex.Message);
            }
        }

        [HttpGet]
        [Route("GreaterThanOrEqual")]
        public IActionResult GreaterThanOrEqual(int age)
        {
            try
            {
                var filter = new BsonDocument
                {{ "Age", new BsonDocument{{ "$gte", age }}}};

                var data = _studentsAge.Find(filter).ToList();

                return Ok(data);
            }
            catch (Exception ex)
            {
                return Ok(ex.Message);
            }
        }

        [HttpGet]
        [Route("GreaterThanAndLessThanQuery")]
        public IActionResult GreaterThanAndLessThanQuery(int age1, int age2)
        {
            try
            {
                var filter = new BsonDocument
                {{ "Age", new BsonDocument{{ "$gte", age1 }, { "$lte", age2 } }}};

                var data = _studentsAge.Find(filter).ToList();

                return Ok(data);
            }
            catch (Exception ex)
            {
                return Ok(ex.Message);
            }
        }
    }
}

Conclusion

In this article, we have learned the aggregation function in MongoDB and created APIs in Dot Net Core. example Less than, Less than or equal, Greater than, and Greater than or equal.

Also, check CRUD Operations In .NET Core API Using MongoDB

Submit a Comment

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

Subscribe

Select Categories