1

I have a collection EmployeeDetails. The collection has 4 fields. I have filter with first three fields. I want to update ($set) if matching data found else insert (.SetOnInsert {upsert}); however in bulk.

EmpName:
EmpCompany:
EmpDesignation:
EmpSalary:

I would like to update EmpSalary on the basis of other fields. Also, filter data will be sent in bulk. Is it possible to so w/o a foreach loop.

I have tried the followed code:

foreach( var filterData in filterDataArrayList)
{
         var loadData = Builders<EmployeeModel>.Update
         .SetOnInsert(x=>x.EmpSalary , Salary)
         .SetOnInsert(x=>x.EmpName , Name)
         .SetOnInsert(x=>x.EmpCompany , Company)
         .SetOnInsert(x=>x.EmpDesignation , Designation)

var insertResult = await collection.UpdateOneAsync(
x=>x.EmpName == filterData.Name, x=>x.EmpCompany=filterData.Company, x=>x.EmpDesignation = filterData.Designation  ), loadData,
new UpdateOptions() {IsUpsert=true});

if(loadData.upsertId==null && loadData.matchedCount==1)
   {
      var updateData = Builders<EmployeeModel>.Update
         .Set(x=>x.EmpSalary , Salary)
      
      var updateResult = await collection.UpdateOneAsync(
      x=>x.EmpName == filterData.Name, x.EmpCompany=filterData.Company, x.EmpDesignation =filterData.Designation  ), updateData)

   }

This code works fine. I want to eliminate foreach loop for filter data. Is that possible?

1 Answer 1

2

Try this:

        var client = new MongoClient();
        var db = client.GetDatabase("d");
        var coll = db.GetCollection<BsonDocument>("c");

        coll.BulkWrite(new[]
        {
            new UpdateOneModel<BsonDocument>(
                "{ whatever1 : 1 }",
                new UpdateDefinitionBuilder<BsonDocument>()
                    .SetOnInsert("field1", 1)
                    .SetOnInsert("field2", 2)),
            new UpdateOneModel<BsonDocument>(
                "{ whatever2 : 1 }",
                new UpdateDefinitionBuilder<BsonDocument>()
                    .SetOnInsert("field21", 1)
                    .SetOnInsert("field22", 2))
                {
                    IsUpsert = true
                }
        });

This example is just to show how it can be done, you can use a typed/more complex way as in your example too

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

4 Comments

here you added 2 filters, but if we have a foreach loop, where we have have 3 filters, then how will this work.
I've added 2 different bulk requests (updateone in this case). Each request is independent. if you need 3 requests, then you should add a new request to the bulkWriteModels collection. However I don't understand what you want to do, your above code is not compiled.
I want to update documents in a collection based on filterData. If data is present, then update it i.e., update 'salary' ; if not present, insert all the details. The issue is, I am sending data in bulk, and applying filter in for loop and then upserting.
@pratyaksh So does replacing your loop with write models in a bulk write not solve your problem? Or is there something else you're not telling us?

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.