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?