MongoWriteException: A write operation resulted in an error. The positional operator did not find the match needed from the query

MongoWriteException: A write operation resulted in an error. The positional operator did not find the match needed from the query

I have a collection Zonedetails shown below. below.

I have a collection Zonedetails shown below. below.

I am using C# to Insert or Update a Unit to Units array. If it is an insert I can insert Area blank. If it is an update It should only update the UnitName.

{ 
    "Code" : "Zone1", 
    "Name" : "ZoneName1", 
    "Units" : [
        {
            "UnitCode" : "Unitcode1", 
            "UnitName" : "UnitCodeName", 
            "Areas" : [
                {
                    "AreaCode" : "AreaCode1", 
                    "AreaName" : "AreaName1"
                }
            ]
        }
    ]
}
{ 
    "Code" : "Zone2", 
    "Name" : "ZoneName2", 
    "Units" : [
        {
            "UnitCode" : "UnitCode2", 
            "UnitName" : "UnityName2", 
            "Areas" : [
                {
                    "AreaCode" : "Areacode2", 
                    "AreaName" : "AreaName2"
                }
            ]
        }
    ]
}
{ 
    "Code" : "Zone3", 
    "Name" : "ZoneName3", 
    "Units" : [
        {
            "UnitCode" : "UnitCode3", 
            "UnitName" : "UnitName3", 
            "Areas" : [
                {
                    "AreaCode" : "Areadcode3", 
                    "AreaName" : "AreaName3"
                }, 
                {
                    "AreaCode" : "AreaCode4", 
                    "AreaName" : "Areaname4"
                }, 
                {
                    "AreaCode" : "AreaCode5", 
                    "AreaName" : "Areaname5"
                }
            ]
        }, 
        {
            "UnitCode" : "UnitCode6", 
            "UnitName" : "UnitName6", 
            "Areas" : [
                {
                    "AreaCode" : "AreaCode10", 
                    "AreaName" : "AreaName10"
                }, 
                {
                    "AreaCode" : "AreaCOde11", 
                    "AreaName" : "AreaName10"
                }, 
                {
                    "AreaCode" : "AreaCode12", 
                    "AreaName" : "AreaName12"
                }
            ]
        }
    ]
}

I have writtent a C# code shown below. But getting "The positional operator did not find the match needed from the query"error if the Unit Code does not exist

    var mongoCollection = _context.GetCollection<Zone>("ZoneDetail");
    var filter = Builders<Zone>.Filter.Where(x => x.Code == zoneCode && x.Units.Any(u => u.UnitCode == unit.UnitCode));
    var updateUnitCode = Builders<Zone>.Update.Set(x => x.Units.ElementAt(-1).UnitCode, unit.UnitCode);
    var updateUnitName = Builders<Zone>.Update.Set(x => x.Units.ElementAt(-1).UnitName, unit.UnitName);
    var result = await mongoCollection.UpdateOneAsync(filter, Builders<Zone>.Update.Combine(updateUnitCode, updateUnitName), new UpdateOptions { IsUpsert = true});


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Variable Introduction in C#[Bangla]

LIKE | COMMENT | SHARE | SUBSCRIBE A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...