locked
Filtering mongodb data RRS feed

  • Question

  • User842257015 posted

    Hi All,

    I used mongodb in my backend to store some customer personal information and i need to fetch the user who ages 30, 32 and 35.

    I tried below ways to get but it returns zero results and i used MongoDB.Driver

    Age = new string[] { "26-30", "31-35" }
    DateTime today = DateTime.Today;
    var filter = Builders<Customer>.Filter.Empty;
    foreach (var item in searchFilterBlock.Age)
     {
        var ageBetween = item.Split('-');
        int.TryParse(ageBetween[0], out int startYear);
        int.TryParse(ageBetween[1], out int endYear);
        var start = today.AddYears(-startYear);
        var end = today.AddYears(-endYear); 
        filter = filter & (Builders<Customer>.Filter.Gte(x => x.Dob, start)
                            & Builders<Customer>.Filter.Lte(x=>x.Dob, end));
     }
    // te execute the filter
    var searchResult = _context.Customer.Find(filter).ToList(); // it return 0 result
    

    Thanks,

    Saturday, June 22, 2019 5:56 AM

All replies