filter data between a date range MongoDB with C#

Viewed 253

Hi I am working on a function where I have to get the data in a file according to the ID and the Start and End date.

This is what I wrote so far.

In controller

[HttpGet]
[Route("searchLogs")]
public async Task<ActionResult> SearchLogs([FromBody] List<Logs> logs)
{
    var response = await logProvider.SearchLogs(logs);
    return Ok(response);
}

In model class

public class Logs
{
    public DateTime Date{get; set;}
    public string Id {get; set;}
}

Can someone help me?

1 Answers

db is type of IMongoDbContext

startDate and endDate is DateTime

 var q =  db.GetCollection<Logs>().Find(x => x.Date >= startDate.Date &&  x.Date <= endDate.date).ToList();
Related