Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-14267

Clarify ambiguous $ positional operator behavior when matching multiple arrays

      Description

      The $ positional operator behaves ambiguously when there are multiple matching arrays. The work tracked on SERVER-18500 has not seen any progress, but we see this ambiguity causing issues with some users in SERVER-54687 and SERVER-53896.

      https://docs.mongodb.com/manual/reference/operator/update/positional/

      We should clarify we currently don't guarantee which array we get the position from when using the positional operator when there are multiple array matches.
      Example:

      doc = { a: [1, 2], z: [1, 2], b: [1, 2] }
      db.coll.insert(doc);
      db.coll.update({ a: 1, z: 2, b: 1 }, { $set: { "b.$": 3 } });
      

      There is ambiguity around which index of the matched arrays (0 or 1) should we update b.
      The current behavior returns:

      { "a" : [ 1, 2 ], "z" : [ 1, 2 ], "b" : [ 1, 3 ] }
      

      where we update b using the index matched in z (index 1).

      We may be able to guide users to consider $[<identifier>] if they need to make changes to an array based on a specified position. An example of favoring $[<identifier>] over $ can be found in my closing comment on SERVER-54687

      Scope of changes

      Impact to Other Docs

      MVP (Work and Date)

      Resources (Scope or Design Docs, Invision, etc.)

            Assignee:
            kenneth.dyer@mongodb.com Kenneth Dyer
            Reporter:
            edwin.zhou@mongodb.com Edwin Zhou
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              2 years, 11 weeks, 3 days ago