.NET Driver | Driver Update calls not register in the database

Hey there good people!

I’ve noticed an issue with driver while trying to update a segment of my document in the database.
I have the following markup for my entities - A WorkDay Object that holds a datetime, 3 Shift Types - each holding a List

    "_id": {
        "$oid": "5f376d7ca1787ac4d407c891"
    "Date": {
        "$date": "2020-07-11T21:00:00.000Z"
    "DayShift": {
        "Employees": []
    "EveningShift": {
        "Employees": []
    "NightShift": {
        "Employees": []

I’m calling an API to make an update and pass the _id of the workday and a list of employee Ids that I want present at that particular shift on that day.

public async Task UpdateDayShift(string workDayId, List<string> empIds)
            // get all employees
            var employees = await _employees.AsQueryable<Employee>().ToListAsync();
            // get current instance of the workday from the database
            var newWorkDay = await _workDays.FindAsync<WorkDay>(d => d.Id == workDayId).Result.FirstAsync();
            // clear content of dayshift
            // add only the selected employees into the shift 
            foreach (var e in employees)
                if (empIds.Contains(e.Id))
            // set filter and update params
            var filter = Builders<WorkDay>.Filter.Eq("_id", newWorkDay.Id);
            var update = Builders<WorkDay>.Update.Set(f => f.DayShift, newWorkDay.DayShift);

            //update shift
            await _workDays.FindOneAndUpdateAsync(filter, update);

I know for a fact the parameters I give are valid through debugging, and that there’s an established connection.
My API also returns a 200 code, but no updates actually occur on the database.

Has anyone encountered this issue?

Kind regards,

Hi @Dani_Rashba, and welcome to the forums!

Without a provided class mapping, I’d guess that the issue here is because your update filter does not match any documents. This is likely because the below filter:

var filter = Builders<WorkDay>.Filter.Eq("_id", newWorkDay.Id);

Only trying to match where the value of field _id is equivalent to string of id. The value of _id based on your example document, is in ObjectId format. In order to convert string to an ObjectId you could utilise ObjectId.Parse() method. For example:

var workDayId = ObjectId.Parse("5f3f2a80040283351015ee93"); 

As an side note to your question and depending on the application use case, you could alter the data modelling for optimisation.

Currently you’re duplicating employee records, in the employee collection and in the workdays collection. Depending on the use case, this may not be ideal if there is an update to the employee contact number in the employee collection that is no propagated properly in the workday collection. You could try just saving the _id value of employees in workday collection, and use $lookup to view both information together. Removing this duplicate of information, will also saves your application from fetching the entire employee collection just to perform a single update.

Another consideration that is depending on the use case is to insert a new workday entry instead of updating an existing one. The benefit here is to have a historical records of shifts. For example, whether employee X was on duty last week during the day, etc.

See also Building With patterns: A Summary for various design pattern examples.



Hi Wan,

Thank you very much, I’ve already resolved the issue after figuring out I was updating a null document.
The issue was exactly as you said, the _id was in fact - wrong.

As for optimization - I definitely agree with you, right now I’m playing with .NET’s blazor wasm and trying to get an MVP for my team at work, there’s a lot to optimize on the app since right now I have an API call for each shift update while usually, a work week is scheduled for the whole week (7 days X 3 Shifts) of API update calls. I need to learn how to batch update with mongo let alone find a good way to to send all data at once.

Warm regards,

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.