0

I'm having a bug in mongoDb when I update a list that is inside another array

First code down is method responsible for change

First image representation of my json file, I need to change dependentBenefits

ERROR RETURN: Too many positional (i.e. '$') elements found in path 'dependents.$.dependents Benefits.$.type.required

enter image description here

   public string UpdateBenefits(string ownerId, string benefitsId, Benefits DocumentsIn)
        {

            _employee.FindOneAndUpdate(
               c => c.ownerId == ownerId && c.dependents.Any(s => s.Id == benefitsId),
               Builders<Employee>.Update.Set(c => c.dependents.ElementAt(-1).dependentsBenefits.ElementAt(-1), DocumentsIn));


            return benefitsId;

        }
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Eliemerson Fonseca
  • 717
  • 3
  • 10
  • 33
  • 1
    You can take a look at this answer - it's the same reason, and the same solution should work for you: https://stackoverflow.com/questions/55597781/expression-tree-is-not-supported-on-updateoneasync/55598918#55598918 – mickl Apr 13 '19 at 22:41
  • I used the same example but in my scenario: "dependents. $ [Dependent] .dependentsBenefits. $ [Benefits]" I get ErrorMessage = "No array filter found for identifier 'dependent' in path 'dependents.$[dependent].dependentsBenefits.$[benefits]'" – Eliemerson Fonseca Apr 14 '19 at 02:39
  • 1
    Positional `$` cannot be used multiple times within a statement and can only be used **once**. The modern solution is [positional filtered `$[]`](https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/), but you should also strongly consider **not nesting arrays** as such structures are basically not possible to atomically update until recent versions and are still notoriously difficult to query. – Neil Lunn Apr 14 '19 at 04:33
  • I'm sorry, but I did not understand one of both examples. It is not the same as the errors are different. If you knew, you could give me an example ? – Eliemerson Fonseca Apr 14 '19 at 04:58

0 Answers0