1

I have a Shipment object with a nested list of materials objects (note : _id refers to the shipmentId)

"_id" : "1",
"createddate" : "2018-10-18T16:25:59.245Z",
"attributes" : {       
        "materials" : [ 
            {
                "materialcode" : "string",             
                "status" : "Unmatched"
            }
        ]
    },

I would like to delete the the material of the corresponding shipment if the status = Unmatched. I am trying to use pull or pullfilter just not sure how to use it

here is what I have so far, (this does not build as I get error cannot covert lamda expression to field expression ):

Task<bool> DeleteShipmentMaterials(string ShipmentId, string MaterialId)
        {
       var shipment = await GetShipment(ShipmentId);
                foreach (var material in shipment.attributes.Materials)
                {
                    var update = Builders<Shipment>.Update.PullFilter(x => x.attributes.Materials, Builders<Shipment>.Filter.Eq(x => x.Id, shipment.Id));
                 .MaterialCode == material.MaterialCode),  );
                    await UpdateOneAsync(c => c.Id == shipment.Id, update, true);
                }

1 Answer 1

1

No need to use foreach loop here. You can just use $pull an specify a condition - this will remove all the elements with specified status. In Mongo shell you can run:

db.shipments.update(
    { "_id" : "1" },
    { $pull: { "attributes.materials": { "status": "Unmatched" } } }
)

In C# code you can use PullFilter method to build your filter. Try:

var filter = Builders<Shipment>.Filter.Eq(x => x._id, "1");
var update = Builders<Shipment>.Update.PullFilter(x => x.attributes.materials, 
    material => material.status == "Unmatched");

Col.UpdateOne(filter, update);
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.