Mongodb C#find:如何执行小于等于或大于等于的日期过滤器 [英] Mongodb C# find : how to execute date filters with less than and equal to or greater than equal to

查看:256
本文介绍了Mongodb C#find:如何执行小于等于或大于等于的日期过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能一直在努力使用C#为mongo数据库查询执行一个查询。

我正在使用最新版本2.2.4 mongochsharpdriver。

我尝试了以下方法。



我的尝试:



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:

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);





以这种方式,我无法执行GTE或LTE进行日期检查。尝试另一个如下。





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()));





这样,我无法转换日期,获取序列化异常。我的要求是如果属性类型是日期,则应构造查询以获取属性日期的GTE。任何人都可以帮忙解决问题吗?



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?

推荐答案

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()));
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()));





通过这种方式,我无法转换日期,获得序列化异常。我的要求是如果p roperty类型是日期,查询应该构造为获取属性日期的GTE。任何人都可以帮忙解决问题吗?



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?


你不应该使用字符串值,使用正确的日期类型:参见 BSON类型 - MongoDB手册3.2 [ ^ ]。
You should not be using string values, use the proper date type: see BSON Types - MongoDB Manual 3.2[^].


这篇关于Mongodb C#find:如何执行小于等于或大于等于的日期过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆