[CSHARP-1677] How to execute date filters with less than and equal to or greater than equal to Created: 26/May/16  Updated: 26/Sep/22  Resolved: 06/Jan/17

Status: Closed
Project: C# Driver
Component/s: API, Serialization
Affects Version/s: 2.2.4
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: sivakumar [X] Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB, mongocsharpdriver 2.2.4



 Description   

Hi,
I could have been struggling to execute one query using C# for mongo database find query.

I have tried the following ways.

List<BsonElement> searchQuery = new List<BsonElement>();
foreach (PropertyInfo prop in Model.GetType().GetProperties())
                {
                    if (prop.GetValue(Model) != null && !string.IsNullOrEmpty(prop.GetValue(Model).ToString()))
                    {
                        if (prop.PropertyType.FullName.Contains("Int32"))
                            searchQuery.Add(new BsonElement(prop.Name, Convert.ToInt32(prop.GetValue(Model))));
                        else if (prop.PropertyType.FullName.Contains("DateTime"))
                            searchQuery.Add(new BsonElement(prop.Name, Convert.ToDateTime(Convert.ToDateTime(prop.GetValue(Model)).ToShortDateString())));
                        else
                            searchQuery.Add(new BsonElement(prop.Name, prop.GetValue(Model).ToString()));
                      
                    }
                }
 
   var query = new QueryDocument(searchQuery);
                var result = _propertyRepository.Find(query);

In this manner, i couldn't be able to execute the GTE or LTE for date check.

Tried another one as below.

StringBuilder str1 = new StringBuilder();
foreach (PropertyInfo prop in Model.GetType().GetProperties())
                {
                    if (prop.GetValue(Model) != null && !string.IsNullOrEmpty(prop.GetValue(Model).ToString()))
                    {
                        
                        if (prop.PropertyType.FullName.Contains("Int32"))
                        {
                            if (Convert.ToInt32(prop.GetValue(Model)) > 0)
                                str1.Append("{" + prop.Name + ":" + Convert.ToInt32(prop.GetValue(Model)) + " }");
                        }
                        else if (prop.PropertyType.FullName.Contains("DateTime"))
                            str1.Append("{" + prop.Name + ":{$gt : " + Convert.ToDateTime(prop.GetValue(Model)).ToShortDateString().ToString() + " }}");
                        else
                            str1.Append("{" + prop.Name + ":\"" + prop.GetValue(Model).ToString() + "\"}");
                    }
                }
var query = new QueryDocument(BsonSerializer.Deserialize<BsonDocument>(str1.ToString()));

In this manner, i am not able to convert the date, getting serialization exception.

My requirement is if the property type is date, the query should construct to get GTE of the property date.

Could any one help to fix the issue?

Thank you



 Comments   
Comment by Robert Stam [ 06/Jan/17 ]

Using the latest version of the driver you can use code like the following to build up a filter dynamically by "and"-ing together multiple clauses.

Start with an empty filter:

var builder = Builders<BsonDocument>.Filter; // or use your own C# class instead of BsonDocument
var filter = builder.Empty;

And for each clause that you want to "and" together use code like the following:

var name = "name";
var dateTime = new DateTime(2016, 12, 1, 0, 0, 0, DateTimeKind.Utc); // must be in UTC
var clause = builder.Gte(name, dateTime);
filter = builder.And(filter, clause);

The clauses can be anything. I used a GTE comparison against a DateTime field since that seems to be the one you were most interested in.

Generated at Wed Feb 07 21:40:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.