public void Save(DeviceCommand command) { if (command == null) { throw new ArgumentNullException("command"); } if (command.Device != null) { command.DeviceID = command.Device.ID; } if (command.ID == default(int)) { // use findAndModify to get the database-generated timestamp in response _mongo.SetIdentity(command, _mongo.GetIdentityFromBlock(typeof(DeviceCommand).Name)); var result = _mongo.DeviceCommands.FindAndModify(new FindAndModifyArgs { Query = Query <DeviceCommand> .EQ(e => e.ID, command.ID), Update = Update.Combine( command.Timestamp == default(DateTime) ? Update <DeviceCommand> .CurrentDate(e => e.Timestamp) : Update <DeviceCommand> .Set(e => e.Timestamp, command.Timestamp), Update <DeviceCommand> .Set(e => e.Command, command.Command), Update.Set("Parameters", command.Parameters == null ? BsonNull.Value : BsonSerializer.Deserialize <BsonValue>(command.Parameters)), Update <DeviceCommand> .Set(e => e.DeviceID, command.DeviceID), Update <DeviceCommand> .Set(e => e.UserID, command.UserID), Update <DeviceCommand> .Set(e => e.Lifetime, command.Lifetime), Update <DeviceCommand> .Set(e => e.Status, command.Status), Update <DeviceCommand> .Set(e => e.Result, command.Result)), Upsert = true, VersionReturned = FindAndModifyDocumentVersion.Modified, Fields = Fields <DeviceCommand> .Include(e => e.Timestamp), }); _mongo.SetTimestamp(command, result.ModifiedDocument["Timestamp"].ToUniversalTime()); } else { _mongo.DeviceCommands.Save(command); } }
public void Save(DeviceNotification notification) { if (notification == null) { throw new ArgumentNullException("notification"); } if (notification.Device != null) { notification.DeviceID = notification.Device.ID; } if (notification.ID == default(int)) { // use findAndModify to get the database-generated timestamp in response _mongo.SetIdentity(notification, _mongo.GetIdentityFromBlock(typeof(DeviceNotification).Name)); var result = _mongo.DeviceNotifications.FindAndModify(new FindAndModifyArgs { Query = Query <DeviceNotification> .EQ(e => e.ID, notification.ID), Update = Update.Combine( notification.Timestamp == default(DateTime) ? Update <DeviceNotification> .CurrentDate(e => e.Timestamp) : Update <DeviceNotification> .Set(e => e.Timestamp, notification.Timestamp), Update <DeviceNotification> .Set(e => e.Notification, notification.Notification), Update.Set("Parameters", notification.Parameters == null ? BsonNull.Value : BsonSerializer.Deserialize <BsonValue>(notification.Parameters)), Update <DeviceNotification> .Set(e => e.DeviceID, notification.DeviceID)), Upsert = true, VersionReturned = FindAndModifyDocumentVersion.Modified, Fields = Fields <DeviceNotification> .Include(e => e.Timestamp), }); _mongo.SetTimestamp(notification, result.ModifiedDocument["Timestamp"].ToUniversalTime()); } else { _mongo.DeviceNotifications.Save(notification); } }