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

Update and $ operator mismatch with multiple arrays in query

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.4.1, 3.4.7, 3.5.12
    • Component/s: Querying
    • Labels:
      None
    • ALL

      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.

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            hmducoulombier@marketing1by1.com Henri-Maxime Ducoulombier
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: