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

Order of operators matters when using $each with $push

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.5.2
    • Affects Version/s: 2.4.4
    • Component/s: Write Ops
    • None
    • Environment:
      Ubuntu 12.04 LTS
    • Fully Compatible
    • ALL
    • Hide
      db.test_update.insert({name: "2.4"})
      // this works
      db.test_update.update({name :"2.4"}, { $push: { n: { $each: [{x:10},{x:4}, {x:1}] , $slice: -1 , $sort: { x:1 }}}})
      db.test_update.findOne()
      {
      	"_id" : ObjectId("51c8a7f31c7a4e0a10e4f14a"),
      	"n" : [
      		{
      			"x" : 10
      		}
      	],
      	"name" : "2.4"
      }
      // reset things
      db.test_update.update({name:"2.4"},{ $set : { n : [] }})
      // this does not work
      db.test_update.update({name :"2.4"}, { $push: { n: { $slice : -1, $each: [{x:10},{x:4}, {x:1}] , $sort: { x:1 }}}})
      db.test_update.findOne()
      {
      	"_id" : ObjectId("51c8a7f31c7a4e0a10e4f14a"),
      	"n" : [
      		{
      			"$slice" : -1,
      			"$each" : [
      				{
      					"x" : 10
      				},
      				{
      					"x" : 4
      				},
      				{
      					"x" : 1
      				}
      			],
      			"$sort" : {
      				"x" : 1
      			}
      		}
      	],
      	"name" : "2.4"
      }
      
      Show
      db.test_update.insert({name: "2.4" }) // this works db.test_update.update({name : "2.4" }, { $push: { n: { $each: [{x:10},{x:4}, {x:1}] , $slice: -1 , $sort: { x:1 }}}}) db.test_update.findOne() { "_id" : ObjectId( "51c8a7f31c7a4e0a10e4f14a" ), "n" : [ { "x" : 10 } ], "name" : "2.4" } // reset things db.test_update.update({name: "2.4" },{ $set : { n : [] }}) // this does not work db.test_update.update({name : "2.4" }, { $push: { n: { $slice : -1, $each: [{x:10},{x:4}, {x:1}] , $sort: { x:1 }}}}) db.test_update.findOne() { "_id" : ObjectId( "51c8a7f31c7a4e0a10e4f14a" ), "n" : [ { "$slice" : -1, "$each" : [ { "x" : 10 }, { "x" : 4 }, { "x" : 1 } ], "$sort" : { "x" : 1 } } ], "name" : "2.4" }

      Essentially, when using $each with $push, the $each operator must come before any other operands such as $slice or $sort. This is breaking in many languages since dicts/objects do not maintain a consistent key ordering.

            Assignee:
            scotthernandez Scott Hernandez (Inactive)
            Reporter:
            ej.bensing@10gen.com E.J. Bensing
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: