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

Update and $ operator mismatch with multiple arrays in query

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 3.4.1, 3.4.7, 3.5.12
    • Fix Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Operating System:
      ALL

      Description

      Consider a simple collection with a single document, build this way :

      ("a" and "a2" fields are used to demonstrate the update behavior below)

      db.mongo.insert(
      {
      	"a":[NumberInt(123), NumberInt(456), NumberInt(789)],
      	"a2": NumberInt(456),
      	"b":{
      		"c":[
      			{"d":NumberInt(10), "e":NumberInt(20)},
      			{"d":NumberInt(10), "e":NumberInt(21)},
      			{"d":NumberInt(10), "e":NumberInt(22)},
      			{"d":NumberInt(10), "e":NumberInt(23)},
      			{"d":NumberInt(10), "e":NumberInt(24)},
      			{"d":NumberInt(11), "e":NumberInt(25)},
      			{"d":NumberInt(11), "e":NumberInt(26)},
      			{"d":NumberInt(11), "e":NumberInt(27)},
      		] 
      	} 
      }
      );
      

      My goal is to update array b.c and add elements to a set where d = 10 and e = 24, only for document where a = 456.

      This first query does not work : it updates the element in array b.c with the position of "456" in the a array ( = the second element), even with the $elemMatch on b.c.

      db.mongo.update(
      	{
      	 	"a": NumberInt(456),
      	 	"b.c": {
      	 	  "$elemMatch": {
      	 	    "d": NumberInt(10),
      	 	    "e": NumberInt(24)
      	 	    }
      	 	  }
      	},
      	{
      		"$addToSet":{
      			"b.c.$.f":{"x":NumberInt(1), "y":NumberInt(2)}
      		}
      	}
      );
      

      This second query does work : it updates the correct element in array because a2 is not an array. This issue being, I need a to be an array and hold several values.

      db.mongo.update(
      	{
      	 	"a2": NumberInt(456),
      	 	"b.c": {
      	 	  "$elemMatch": {
      	 	    "d": NumberInt(10),
      	 	    "e": NumberInt(24)
      	 	    }
      	 	  }
      	},
      	{
      		"$addToSet":{
      			"b.c.$.f":{"x":NumberInt(1), "y":NumberInt(2)}
      		}
      	}
      );
      

      I have tried to change the order of the conditions in the query part, but this does not change the behavior.

      Any help (or even better, a fix..) would be greatly appreciated.

      Tested on 3.4.1, 3.4.7 and 3.5.12 (to see if the changes in SERVER-831 would fix it.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                1 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: