-
Type:
Task
-
Resolution: Done
-
Priority:
Major - P3
-
None
-
Affects Version/s: 2.2.4
-
Component/s: API, Serialization
-
Environment:MongoDB, mongocsharpdriver 2.2.4
-
None
-
None
-
None
-
None
-
None
-
None
-
None
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
- mentioned in
-
Page Loading...