Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I could have been struggling to execute one query using C# for mongo database find query.
I am using Latest version 2.2.4 mongochsharpdriver.
I have tried the following ways.

What I have tried:

C#
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.

C#
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?
Posted
Updated 29-May-16 21:39pm
v2

1 solution

You should not be using string values, use the proper date type: see BSON Types - MongoDB Manual 3.2[^].
 
Share this answer
 
Comments
Kumarbs 30-May-16 4:53am    
Tried that date type also. But got the same serialization exception.

str1.Append("{" + prop.Name + ":{$gt : " + Convert.ToDateTime(prop.GetValue(Model)) + " }}");
Richard MacCutchan 30-May-16 4:56am    
Surely that property is alreasdy a DateTime?
Kumarbs 30-May-16 5:33am    
Yes, it is as datetime. But in conversion it is not able to convert as BsonValue
Richard MacCutchan 30-May-16 7:08am    
I can see that, but you cannot expect me (or anyone else here) to guess what is happening in your application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900