3

I am working on MongoDb using C# drivers. I want query the mongoDb database to find out the row which has EventDate is greater than 13 months from todays date.

my MongoDb has structure something similar to below:

EventDate is of datatype : 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.

2 Answers 2

4

There's more than one way to it, but this would be the one with LINQ extension method syntax:

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the query. I am trying to delete those record which I got in above query. PLEASE CAN UPDATE YOUR ANSWER.
to remove records, just change Find to Remove
could you explain to me what the 'p' is?? I'm still new to mongo and linq
@M009: Yes, but not in a comment to a one year old question. Please create a new question for it.
0

DateTime.UtcNow.AddMonths(-13) is a good example and works perfect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.