c# - How does custom serializer affect searching? -
i trying use nodatime in app. app persists data in mongodb database. consider following class
public class sometype { public objectid id { get; set; } public instant instant { get; set; } [bsondatetimeoptions(kind = datetimekind.local)] public datetime datetime { get; set; } [bsondatetimeoptions(kind = datetimekind.utc)] public datetime datetimeutc { get; set; } // public zoneddatetime zoneddatetime { get; set; } // public localdatetime localdatetime { get; set; } }
without adding custom serializer, instant
property of class doesn't stored in db. reading document db fails.
public class instantbsonserializer : bsonbaseserializer { public override object deserialize(bsonreader bsonreader, type nominaltype, ibsonserializationoptions options) { var ticks = bsonreader.readint64(); return new instant(ticks); } public override object deserialize(bsonreader bsonreader, type nominaltype, type actualtype, ibsonserializationoptions options) { var ticks = bsonreader.readint64(); return new instant(ticks); } public override void serialize(bsonwriter bsonwriter, type nominaltype, object value, ibsonserializationoptions options) { var instant = (instant) value; bsonwriter.writeint64(instant.ticks); } }
i created above serializer , registered properly. able save , retrieve instances of class proper value set instant
.
my question how c# driver handle searching using linq?
var list = mongodatabase.getcollection<sometype>("sometype") .asqueryable<sometype>() .where(x => x.instant < instant.fromdatetimeutc(datetime.touniversaltime())) .tolist(); console.writeline(list.count);
i correct list of documents. i trying understand how mongodb gets data. first load data, deserialize , filter? or serialize instant
value of clause , use serialized value filter documents, load matching ones , deserialize?
i tried seeing query logged mongodb profiler, doesnt seem log anything. have set profiling level 2.
in case of instant
serialized value long
. in case of zoneddatetime
, serialized value document 2 properties. how searching work in case?
edit:
i able profiling work. following c# query,
mongodatabase.getcollection<sometype>("sometypecollection") .asqueryable<sometype>() .where(x => x.instant < instant.fromdatetimeutc(datetime.utcnow)) .tolist();
results in following mongodb query
{ "instant": { $lt: 13781017828460782 }}
meaning, c# driver serializes instant
object , uses serialized value filter results in db itself.
the driver convert linq query mongodb query , filter results before deserialising them. query not run until call tolist()
here operators supported: http://docs.mongodb.org/ecosystem/tutorial/use-linq-queries-with-csharp-driver/
the custom deserialiser not come play until driver starts building object graph data returned.
you wouldn't need custom serialiser classes simple types or you're not doing work when reading/writing docs.
if wanted control serialised, ie if class holds single datetime, why not make utc datetime stamp stored , use bsonignore
other properties.
Comments
Post a Comment