0

I want to upsert the complete document except Id field with C# MongoDB official driver 2.4.4 From API documention I can create UpdateDefinination as shown below

var filter = Builders<Shipment>.Filter.Where(_ => _.ShipmentIdentifier == shipment.ShipmentIdentifier);
var options = new FindOneAndUpdateOptions<Shipment>() { IsUpsert = true };
var update = BuildUpdateDefination(shipment);
var updatedEntity =  collection.FindOneAndUpdate(filter, update, options);

This Code works fine with below BuildUpdateDefination

 private UpdateDefinition<Shipment> BuildUpdateDefination(Shipment shipment)
    {
        var update = Builders<Shipment>.Update.Set(_ => _.Carrier, shipment.Carrier)
            .Set(_ => _.Addresses, shipment.Addresses)
            .Set(_ => _.Attributes, shipment.Attributes)
            .Set(_ => _.CarrierCashOnDeliveryAmount, shipment.CarrierCashOnDeliveryAmount)
            .Set(_ => _.CarrierInsuranceAmount, shipment.CarrierInsuranceAmount)
            .Set(_ => _.CarrierRoutingError, shipment.CarrierRoutingError)
            .Set(_ => _.CarrierRoutingValue1, shipment.CarrierRoutingValue1)
            .Set(_ => _.CarrierRoutingValue2, shipment.CarrierRoutingValue2)
            .Set(_ => _.CarrierRoutingValue3, shipment.CarrierRoutingValue3)
            .Set(_ => _.CarrierRoutingValue4, shipment.CarrierRoutingValue4)
            .Set(_ => _.CarrierRoutingValue5, shipment.CarrierRoutingValue5)
            .Set(_ => _.CarrierService, shipment.CarrierService)
            .Set(_ => _.CarrierServiceAttributes, shipment.CarrierServiceAttributes)
            .Set(_ => _.CashOnDeliveryReference, shipment.CashOnDeliveryReference)
            .Set(_ => _.Currency, shipment.Currency)
            .Set(_ => _.DeliveryInstruction1, shipment.DeliveryInstruction1)
            .Set(_ => _.DeliveryInstruction2, shipment.DeliveryInstruction2)
            .Set(_ => _.DeliveryInstruction3, shipment.DeliveryInstruction3);
        return update;
    }

But the problem is Shipment has many fields that need to be update. So whats better way to update the all the property of object ?

I have tried to iterate all the properties of the object as below, but this is also not working

var updateSet = new List<UpdateBuilder>();
Type type = shipment.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
   if (!property.Name.Equals("Id") && !property.Name.Equals("ShipmentIdentifier"))
      updateSet.Add(Update.Set(property.Name, (BsonValue)property.GetValue(shipment) ?? BsonNull.Value));

 }
var update = Update<Shipment>.Combine(updateSet).ToBsonDocument(); 

I have been searching from the long time but not got any working solution.

1 Answer 1

1

Did you try Collection.FindOneAndReplace()? you can use it to replace the object and set the isUpsert property to upsert the document.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.