namespace Bts.DataStream; using MongoDB.Bson; using System; public static class MongoDbExtensions { public static BsonArray GetArray(this BsonDocument doc, string elementName) => doc[elementName].AsBsonArray; public static BsonArray? GetArrayOrDefault(this BsonDocument doc, string elementName) => doc.TryGetValue(elementName, out var val) && val.IsBsonArray ? val.AsBsonArray : null; public static string GetString(this BsonDocument doc, string elementName) => doc[elementName].AsString; public static string? GetStringOrDefault(this BsonDocument doc, string elementName) => doc.TryGetValue(elementName, out var val) && val.IsString ? val.AsString : null; public static int GetInt32(this BsonDocument doc, string elementName) => doc[elementName].AsInt32; public static int? GetInt32OrDefault(this BsonDocument doc, string elementName) => doc.TryGetValue(elementName, out var val) && val.IsInt32 ? val.AsInt32 : null; public static long GetInt64(this BsonDocument doc, string elementName) => doc[elementName].AsInt64; public static long? GetInt64OrDefault(this BsonDocument doc, string elementName) => doc.TryGetValue(elementName, out var val) && val.IsInt64 ? val.AsInt64 : null; public static double GetDouble(this BsonDocument doc, string elementName) => doc[elementName].AsDouble; public static double? GetDoubleOrDefault(this BsonDocument doc, string elementName) => doc.TryGetValue(elementName, out var val) && val.IsDouble ? val.AsDouble : null; public static DateTime GetDateTime(this BsonDocument doc, string elementName) => doc[elementName].ToUniversalTime(); public static DateTime? GetDateTimeOrDefault(this BsonDocument doc, string elementName) => doc.TryGetValue(elementName, out var val) && val.IsValidDateTime ? val.ToUniversalTime() : null; public static DateTime GetTimestamp(this BsonDocument doc, string elementName) => doc[elementName].ToUniversalTime(); public static DateTime? GetTimestampOrDefault(this BsonDocument doc, string elementName) => doc.TryGetValue(elementName, out var val) && val.IsValidDateTime ? val.ToUniversalTime() : null; public static ObjectId GetObjectId(this BsonDocument doc, string elementName) => doc[elementName].AsObjectId; public static ObjectId? GetObjectIdOrDefault(this BsonDocument doc, string elementName) => doc.TryGetValue(elementName, out var val) && val.IsObjectId ? val.AsObjectId : null; }