c# - On searching documents in mongodb collection using ISODate range, I get the error "String was not recognized as a valid DateTime" -


i trying query mongodb using following -

list<bsondocument> list = nosqlbusinessentitybase.loadbywhereclause("peoplecounting",     string.concat("{siteid:\"", siteid, "\", locationid:\"", location._id ,"\",     starttime: {$gte:isodate(\"",old.tostring("yyyy-mm-dd hh:mm:ss"),"\")},      endtime: {$lte:isodate(\"",current.tostring("yyyy-mm-dd hh:mm:ss"), "\"\")}}")); 

the loadbywhereclause() function follows -

public static list<bsondocument> loaddatabywhere(string table, string whereclause) {     var collection = db.getcollection(table);     querydocument wheredoc = new querydocument(bsondocument.parse(whereclause));     var resultset = collection.find(wheredoc);     list<bsondocument> docs = resultset.tolist();      if (resultset.count() > 0)     {         foreach (bsondocument doc in docs)         {             doc.set("_id", doc.getelement("_id").tostring().split('=')[1]);         }         return docs;     }     else     {         return null;     } } 

even though query runs fine in mongodb console , returns documents

db.peoplecounting.find({siteid:"53f62abf66455068373665ff", locationid:"53f62bb86645506837366603",     starttime:{$gte:isodate("2012-12-03 02:40:00")}, endtime:{$lte:isodate("2013-12-03 07:40:00")}} 

i error when try load in c# using loadbywhereclause function. error string not recognized valid datetime. while parsing whereclause. how can possibly fix this? unable determine going wrong here.

it's not entirely clear, suspect problem may how you're formatting date. this:

old.tostring("yyyy-mm-dd hh:mm:ss") 

should this:

old.tostring("yyyy-mm-dd hh:mm:ss") 

or possibly

old.tostring("yyyy-mm-dd't'hh:mm:ss") 

because:

  • mm means minutes. don't want minutes value between year , day-of-month; want month (mm)
  • hh means "hour of half-day" (i.e. 1-12). want hour of full day, 0-23 (hh)
  • iso-8601 uses t literal separate date frmo time.

i note current.tostring better, not correct - gets month right, not hour. fact these inconsistent problem start - advise write separate method convert datetime appropriately.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -