public UnsafeStream(BsonHandle bson) : this(bson.GetBsonBuffer()) { if (bson.IsInvalid) { throw new InvalidOperationException(); } _bson = bson; }
/// <summary> /// Loads JSON object identified by OID from the collection. /// </summary> /// <remarks> /// Returns <c>null</c> if object is not found. /// </remarks> /// <param name="id">Id of an object</param> public BsonDocument Load(ObjectId id) { using (var bson = new BsonHandle(() => _functions.LoadBson(CollectionHandle, ref id), Database.Library)) { //document does not exists if (bson.IsInvalid) { return(null); } using (var stream = Database.Library.ConvertToStream(bson)) { return(new BsonDocument(stream)); } } }
/// <summary> /// Loads JSON object identified by OID from the collection. /// </summary> /// <remarks> /// Returns <c>null</c> if object is not found. /// </remarks> /// <param name="id">Id of an object</param> public TDocument Load <TDocument>(ObjectId id) { using (var bson = new BsonHandle(() => _functions.LoadBson(CollectionHandle, ref id), Database.Library)) { //document does not exists if (bson.IsInvalid) { return(default(TDocument)); } using (var stream = Database.Library.ConvertToStream(bson)) using (var reader = new BsonReader(stream)) { var document = Serializer.Deserialize <TDocument>(reader); IdHelper <TDocument> .SetId(document, ref id); return(document); } } }
internal Stream ConvertToStream(BsonHandle bson) { var stream = new UnsafeStream(bson); return(stream); }