How to Get Min and Max DateTime from MongoDB using c# queries?

I'm guessing that your question can be rephrased as follows.

I'm guessing that your question can be rephrased as follows: I have a collection where some documents have an Entry field and some don't I want to find the smallest and largest values present in the collection for those documents that actually have a value for the Entry field. It is often best to figure things out in the MongoDB shell first and then translate to C# after you're done experimenting. The following shell statements create some sample data: > db.test.remove() > db.test.

Insert({}) > db.test. Insert({Entry:ISODate("2011-12-01T00:00:00Z1")}) > db.test. Insert({Entry:ISODate("2011-12-01T00:00:00Z0")}) > db.test.

Insert({Entry:ISODate("2011-12-01T00:00:00Z1")}) This is what the documents just inserted look like: > db.test.find() { "_id" : ObjectId("4efa1208a981bdd43ddcacd0") } { "_id" : ObjectId("4efa1215a981bdd43ddcacd1"), "Entry" : ISODate("2011-12-01T00:00:00Z") } { "_id" : ObjectId("4efa1218a981bdd43ddcacd2"), "Entry" : ISODate("2011-12-02T00:00:00Z") } { "_id" : ObjectId("4efa121ba981bdd43ddcacd3"), "Entry" : ISODate("2011-12-03T00:00:00Z") } Notice that one of them doesn't have an Entry field. We can exclude it from the results using an $exists query: > db.test. Find({Entry:{$exists:true}}) { "_id" : ObjectId("4efa1215a981bdd43ddcacd1"), "Entry" : ISODate("2011-12-01T00:00:00Z") } { "_id" : ObjectId("4efa1218a981bdd43ddcacd2"), "Entry" : ISODate("2011-12-02T00:00:00Z") } { "_id" : ObjectId("4efa121ba981bdd43ddcacd3"), "Entry" : ISODate("2011-12-03T00:00:00Z") } Now to find the documents containing the smallest and largest values we can sort twice, once ascending and once descending, and limit the result to one document: > db.test.

Find({Entry:{$exists:true}}). Sort({Entry:1}). Limit(1) { "_id" : ObjectId("4efa1215a981bdd43ddcacd1"), "Entry" : ISODate("2011-12-01T00:00:00Z") } > db.test.

Find({Entry:{$exists:true}}). Sort({Entry:-1}). Limit(1) { "_id" : ObjectId("4efa121ba981bdd43ddcacd3"), "Entry" : ISODate("2011-12-03T00:00:00Z") } Then it's just a matter of extracting the Entry value from the document.

Here's what it looks like in C# code: var server = MongoServer. Create("mongodb://localhost/? Safe=true"); var database = server"test"; var collection = database"test"; collection.Drop(); collection.

Insert(new BsonDocument()); collection. Insert(new BsonDocument("Entry", new DateTime(2011, 12, 1, 0, 0, 0, DateTimeKind. Utc))); collection.

Insert(new BsonDocument("Entry", new DateTime(2011, 12, 2, 0, 0, 0, DateTimeKind. Utc))); collection. Insert(new BsonDocument("Entry", new DateTime(2011, 12, 3, 0, 0, 0, DateTimeKind.

Utc))); var query = Query. Exists("Entry", true); var sortAscending = SortBy. Ascending("Entry"); var sortDescending = SortBy.

Descending("Entry"); foreach (var document in collection. Find(query). SetSortOrder(sortAscending)) { Console.

WriteLine(document.ToJson()); } Console.WriteLine(); var minDocument = collection. Find(query). SetSortOrder(sortAscending).

SetLimit(1).First(); var maxDocument = collection. Find(query). SetSortOrder(sortDescending).

SetLimit(1).First(); var minDateTime = minDocument"Entry". AsDateTime; var maxDateTime = maxDocument"Entry". AsDateTime; Console.

WriteLine("Min Entry = {0}", minDateTime. ToString("o")); Console. WriteLine("Max Entry = {0}", maxDateTime.

ToString("o")); The full test program is at: http://www.pastie.org/3080660.

I should add that in order for this to be efficient with large collections you will want to create an index on Entry. – Robert Stam Dec 28 '11 at 15:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions