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

Positional operator ($) can choose wrong array element when there is a numerical path component

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.5.12
    • Affects Version/s: 3.2.11, 3.4.1
    • Component/s: Querying
    • None
    • Minor Change
    • ALL
    • Hide

      Create an Empty DB
      Create a new Collection
      Add 4 Documents:

      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e1"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lac" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc51")}]}
      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e2"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lpc" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc52")}]}
      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e3"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lac" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc53")}]}
      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e4"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lpc" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc54")}]}
      

      Now Run 4 Queries, each to update the array in exactly the same way:

      db.testing.update({"lac.tankId": ObjectId("587fa5f04f4efe9c42d0dc51"), "loc": [1,1] }, {$set:{"lac.$.test":1}})
      db.testing.update({"lpc.tankId": ObjectId("587fa5f04f4efe9c42d0dc52"), "loc": [1,1] }, {$set:{"lpc.$.test":1}})
      db.testing.update({"lac.tankId": ObjectId("587fa5f04f4efe9c42d0dc53"), "loc.0": 1, "loc.1":1 }, {$set:{"lac.$.test":1}})
      db.testing.update({"lpc.tankId": ObjectId("587fa5f04f4efe9c42d0dc54"), "loc.0": 1, "loc.1":1 }, {$set:{"lpc.$.test":1}})
      

      You will have 3 identical and 1 that does not look right.

      Show
      Create an Empty DB Create a new Collection Add 4 Documents: { "_id" : ObjectId("587fa5ed7bc1efc69f4763e1"), "loc" : [ NumberInt(1), NumberInt(1) ], "lac" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc51")}]} { "_id" : ObjectId("587fa5ed7bc1efc69f4763e2"), "loc" : [ NumberInt(1), NumberInt(1) ], "lpc" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc52")}]} { "_id" : ObjectId("587fa5ed7bc1efc69f4763e3"), "loc" : [ NumberInt(1), NumberInt(1) ], "lac" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc53")}]} { "_id" : ObjectId("587fa5ed7bc1efc69f4763e4"), "loc" : [ NumberInt(1), NumberInt(1) ], "lpc" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc54")}]} Now Run 4 Queries, each to update the array in exactly the same way: db.testing.update({"lac.tankId": ObjectId("587fa5f04f4efe9c42d0dc51"), "loc": [1,1] }, {$set:{"lac.$.test":1}}) db.testing.update({"lpc.tankId": ObjectId("587fa5f04f4efe9c42d0dc52"), "loc": [1,1] }, {$set:{"lpc.$.test":1}}) db.testing.update({"lac.tankId": ObjectId("587fa5f04f4efe9c42d0dc53"), "loc.0": 1, "loc.1":1 }, {$set:{"lac.$.test":1}}) db.testing.update({"lpc.tankId": ObjectId("587fa5f04f4efe9c42d0dc54"), "loc.0": 1, "loc.1":1 }, {$set:{"lpc.$.test":1}}) You will have 3 identical and 1 that does not look right.
    • Query 2017-08-21

      Using a different versions of the same query have different results when updating a value in an array.
      If the array key name is alphabetically after the key "loc" field in the query it does not update but insert a blank subdocument.

      changing

      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e3"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lac" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc53")}]}
      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e4"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lpc" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc54")}]}
      

      with:

      db.testing.update({"lac.tankId": ObjectId("587fa5f04f4efe9c42d0dc53"), "loc.0": 1, "loc.1":1 }, {$set:{"lac.$.test":1}})
      db.testing.update({"lpc.tankId": ObjectId("587fa5f04f4efe9c42d0dc54"), "loc.0": 1, "loc.1":1 }, {$set:{"lpc.$.test":1}})
      

      should result in

      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e3"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lac" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc53"), test: 1}]}
      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e4"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lpc" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc54"), test: 1}]}
      

      but ends up with:

      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e3"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lac" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc53"), test: 1}]}
      { "_id" : ObjectId("587fa5ed7bc1efc69f4763e4"),  "loc" : [ NumberInt(1),  NumberInt(1) ],  "lpc" : [ { "tankId" : ObjectId("587fa5f04f4efe9c42d0dc54")}, {test: 1}]}
      

            Assignee:
            tess.avitabile@mongodb.com Tess Avitabile (Inactive)
            Reporter:
            desterhuizen@gmail.com Dawid Esterhuizen
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: