• Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Sharding
    • DB Integration & Observability
    • ALL
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Problem

      An update that changes a document's shard key so that it moves to another shard fails if the document has BSON undefined in any top-level field, including fields that are not part of the shard key:

      During delete stage of updating a shard key :: caused by :: cannot compare to undefined (BadValue, code 2)
      

      The update is rejected and the document stays where it is. Retrying fails the same way, so such a document's shard key can never be changed across shards.

      Reproduction

      Observed on 7.0.46, 2-shard cluster, legacy mongo shell connected to mongos (the legacy shell writes JS undefined as BSON undefined):

      sh.enableSharding("test");
      sh.shardCollection("test.c", {sk: 1});
      sh.splitAt("test.c", {sk: "m"});
      // Put [MinKey, "m") and ["m", MaxKey) on different shards, e.g.:
      // sh.moveChunk("test.c", {sk: "a"}, "<shard0>"); sh.moveChunk("test.c", {sk: "z"}, "<shard1>");
      
      db.getSiblingDB("test").c.insert({_id: 1, sk: "a", f: 1});
      db.getSiblingDB("test").c.insert({_id: 2, sk: "a", f: undefined});
      
      const s = db.getMongo().startSession();
      s.startTransaction();
      s.getDatabase("test").runCommand({update: "c", updates: [{q: {_id: 1, sk: "a"}, u: {$set: {sk: "z"}}}]});
      // ok: 1, nModified: 1 (document moves)
      s.getDatabase("test").runCommand({update: "c", updates: [{q: {_id: 2, sk: "a"}, u: {$set: {sk: "z"}}}]});
      // ok: 0, code 2: "During delete stage of updating a shard key :: caused by :: cannot compare to undefined"
      

      Cause

      To move the document, the router deletes the pre-image using the full pre-image document as the delete filter. In 7.0 this is the pre-image as-is (createShardKeyDeleteOp: entry.setQ(updatePreImage), src/mongo/s/commands/document_shard_key_update_util.cpp). At master the filter comes from convertDocumentIntoQuery(), which still appends undefined values verbatim. The matcher rejects equality on undefined (ComparisonMatchExpression: "cannot compare to undefined").

      Wrapping each field in $eq (as suggested in SERVER-137210) does not fix this, since $eq: undefined is rejected too. A top-level undefined value needs to be matched by type, e.g. {$type: "undefined"}.

      Only 7.0.46 was tested; the master code path looks the same.

      Related: SERVER-137210, SERVER-140033.

            Assignee:
            Unassigned
            Reporter:
            Felipe Gasper
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: