Pacman
Pacman

Reputation: 2245

Querying a document in Mongo using DateTime C#

I have a mongo document with DateTime like this:

"_id" : "58064346e74f22124037a607",
    "DateEffective" : "2016-10-18T15:44:01.083Z",

In my C# code I want to query my collection for any document that it's DateEffective is before today's date, here is my Builders:

var filterDefinition = builder.Lt("DateEffective", new BsonDateTime(DateTime.Now))
var result =  collection.Find(filterDefinition).ToList()

my result.Count is 0

any ideas?

Upvotes: 0

Views: 538

Answers (1)

Gopinath Navaneethan
Gopinath Navaneethan

Reputation: 302

As per your document "DateEffective" is not a date, it is string. In string key less than will not work. So change your "DateEffective" to date format

the document should in this format

{
    "_id" : ObjectId("58064346e74f22124037a607"),
    "DateEffective" : ISODate("2016-10-18T15:44:01.083Z")
}

not in this format

{
"_id" : "58064346e74f22124037a607",
"DateEffective" : "2016-10-18T15:44:01.083Z"
}

Upvotes: 1

Related Questions