Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-37058

Update with numeric field names inside an array can cause validation to fail

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 3.4.18, 3.6.9, 4.0.4, 4.1.4
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
    • Fully Compatible
    • ALL
    • v4.0, v3.6, v3.4, v3.2
    • Query 2018-10-08
    • 50

      Full repro:

      (function() {
          const c = db.coll;
          c.insert({'a': [{}]});
          assert.commandWorked(c.createIndex({'a.0.c': 1}));
          // First 0 is array index.
          // Second 0 is a field name.
          c.update({}, {$set: {'a.0.0.b': 'hi'}});
          const res = assert.commandWorked(c.validate(true));
          assert(res.valid, tojson(res));

      Step 1: Insert a document such as:

      {'a': [{}]}

      Step 2: Create an index, such as:

      {'a.0.c': 1}

      The key for the document we inserted above is null.

      Now do an update:

      c.update({}, {$set: {'a.0.0.b': 'hi'}});

      The update driver determines that this change will not affect any indexes, so the index keys don't get recomputed.

      But when we run validate(), they do get recomputed, and as part of that, validation fails
      because we have a numerical field name in an object nested inside an array:

      One way to fix this is to just add a special check to the update code.

            nicholas.zolnierz@mongodb.com Nicholas Zolnierz
            ian.boros@mongodb.com Ian Boros
            0 Vote for this issue
            6 Start watching this issue