简体   繁体   English

MongoDb使用C#查询日期时间

[英]MongoDb Query using C# for date time

I am working on MongoDb using C# drivers. 我正在使用C#驱动程序处理MongoDb。 I want query the mongoDb database to find out the row which has EventDate is greater than 13 months from todays date. 我想查询mongoDb数据库,找出EventDate从今天起大于13个月的行。

my MongoDb has structure something similar to below: 我的MongoDb具有类似于下面的结构:

EventDate is of datatype : DateTime EventDate的数据类型为:DateTime

{
   "_id" : ObjectId("525239e3e9374f1c3ce4123b"),
   "RowId" : 41133552,
   "EventDate" : ISODate("2013-08-19T00:00:28Z"),
   "Product" : "supporttool",
   "Language" : "en",
   "GUID" : "67cd73d4-36bc-4c9f-9a4c-144b38d4e928",
}

Please can anyone help me out to get the MongoCollection for data with event date older than 13 months. 任何人都可以帮助我获取活动日期超过13个月的数据的MongoCollection。

There's more than one way to it, but this would be the one with LINQ extension method syntax: 它的方法不止一种,但这将是LINQ扩展方法语法的方法:

MongoDatabase db = YourMongoDatabaseObject;
var cursor = db.GetCollection<YourClass>("yourClass").Find(
   Query<YourClass>.LT(p => p.EventDate, DateTime.UtcNow.AddMonths(-13));

This will return a cursor to all documents in the "yourClass" collection that have an an EventDate less than 13 months ago and will deserialize them as instances of YourClass . 这将把光标返回到"yourClass"集合中具有少于13个月前的EventDate所有文档,并将它们反序列化为YourClass实例。

DateTime.UtcNow.AddMonths(-13)是一个很好的例子,非常完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM