4

I am using MongoDB and C# 4.0. In MongoDB I store CreatedOn as a DateTime, for example "2011-01-01T01:40:45.041Z". In C# I am using MongoDB drivers, so how can I query the database for a particular day? So far i have done as below...

var fileLogCollection = db.GetCollection<FileLog>(); Document selector = new Document(); selector["CreatedOn"] =dateTimePicker1.Value.Date; var all = fileLogCollection.Find(selector); 

Thanks

6
  • Have you read C# driver tutorial? Commented Jan 1, 2012 at 23:30
  • yup its not work. and i did read it not much help. how can i put the greater than in the selector sorry its a dumb question i am new to this. Commented Jan 1, 2012 at 23:46
  • Being new to anything doesn't excuse you from doing your homework. Read it again. Commented Jan 1, 2012 at 23:53
  • @Sergio i did not wanted to replay to you rudely what are you asking in the first question "so, dosent it work ?" ya it is working i am just want to ask any way. stop giving replays like a agony aunt dude. if you dont want to replay then move on to whatever. Commented Jan 2, 2012 at 0:02
  • So, is it working or not? If it is, what's the question then? Commented Jan 2, 2012 at 0:04

2 Answers 2

8

Your sample code doesn't look like it's using the official C# driver.

Using the official C# driver you would write something like:

var collection = database.GetCollection<FileLog>("logs"); var query = Query.EQ("CreatedOn", dateTimePicker1.Value.Date.ToUniversalTime()); foreach (var document in collection.FindAll(query)) { // process document } 

You also need to make sure you are storing your CreatedOn values as real BSON DateTime values and not as strings.

You also need to keep in mind that DateTime values are stored in UTC with millisecond resolution.

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

Comments

4

I am using the following method to query the database. You could have a try.

var date = DateTime.Parse("2012-12-12"); var docs = _docs.asQueryable().Where(o=>o.CreatedOn >= date && o.CreatedOn < date.AddDays(1)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.