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

disparate behavior of $slice and $elemMatch projection operators

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 3.6.6, 4.0.0
    • Component/s: Querying
    • Labels:
      None
    • ALL
    • Hide
       >db.tests.find().pretty()
      {
              "_id" : ObjectId("5b487b56f00b9013cff5a4dd"),
              "txid" : 0,
              "input" : [
                      {
                              "_id" : ObjectId("5b487b56f00b9013cff5a4de"),
                              "pid" : 0,
                              "value" : 42
                      }
              ],
              "output" : [
                      {
                              "_id" : ObjectId("5b487b56f00b9013cff5a4e2"),
                              "value" : 123,
                              "n" : 0
                      },
                      {
                              "_id" : ObjectId("5b487b56f00b9013cff5a4e1"),
                              "value" : 345,
                              "n" : 1
                      },
                      {
                              "_id" : ObjectId("5b487b56f00b9013cff5a4e0"),
                              "value" : 567,
                              "n" : 2
                      },
                      {
                              "_id" : ObjectId("5b487b56f00b9013cff5a4df"),
                              "value" : 789,
                              "n" : 3
                      }
              ],
              "__v" : 0
      }
      >var cond = {txid: 0}
      >var update = { $set: { ['output.0.value']: 777 } };
      >var opts = { projection: { _id: 0, output: { $slice: [0,1] } }, returnNewDocument: true };
      >db.tests.findOneAndUpdate(cond, update, opts);
      {
              "txid" : 0,
              "input" : [
                      {
                              "_id" : ObjectId("5b487b56f00b9013cff5a4de"),
                              "pid" : 0,
                              "value" : 42
                      }
              ],
              "output" : [
                      {
                              "_id" : ObjectId("5b487b56f00b9013cff5a4e2"),
                              "value" : 777,
                              "n" : 0
                      }
              ],
              "__v" : 0
      }
      >var update = { $set: { ['output.0.value']: 123 } };
      >var opts = { projection: { _id: 0, output: { $elemMatch: { n: 0 } } }, returnNewDocument: true };
      >db.tests.findOneAndUpdate(cond, update, opts);
      {
              "output" : [
                      {
                              "_id" : ObjectId("5b487b56f00b9013cff5a4e2"),
                              "value" : 123,
                              "n" : 0
                      }
              ]
      }
      >
      
      Show
       >db.tests.find().pretty() { "_id" : ObjectId( "5b487b56f00b9013cff5a4dd" ), "txid" : 0, "input" : [ { "_id" : ObjectId( "5b487b56f00b9013cff5a4de" ), "pid" : 0, "value" : 42 } ], "output" : [ { "_id" : ObjectId( "5b487b56f00b9013cff5a4e2" ), "value" : 123, "n" : 0 }, { "_id" : ObjectId( "5b487b56f00b9013cff5a4e1" ), "value" : 345, "n" : 1 }, { "_id" : ObjectId( "5b487b56f00b9013cff5a4e0" ), "value" : 567, "n" : 2 }, { "_id" : ObjectId( "5b487b56f00b9013cff5a4df" ), "value" : 789, "n" : 3 } ], "__v" : 0 } > var cond = {txid: 0} > var update = { $set: { [ 'output.0.value' ]: 777 } }; > var opts = { projection: { _id: 0, output: { $slice: [0,1] } }, returnNewDocument: true }; >db.tests.findOneAndUpdate(cond, update, opts); { "txid" : 0, "input" : [ { "_id" : ObjectId( "5b487b56f00b9013cff5a4de" ), "pid" : 0, "value" : 42 } ], "output" : [ { "_id" : ObjectId( "5b487b56f00b9013cff5a4e2" ), "value" : 777, "n" : 0 } ], "__v" : 0 } > var update = { $set: { [ 'output.0.value' ]: 123 } }; > var opts = { projection: { _id: 0, output: { $elemMatch: { n: 0 } } }, returnNewDocument: true }; >db.tests.findOneAndUpdate(cond, update, opts); { "output" : [ { "_id" : ObjectId( "5b487b56f00b9013cff5a4e2" ), "value" : 123, "n" : 0 } ] } >

      When using the $slice projection operator on the output field below, the input and txid fields are included in the projection. When using the $elemMatch projection operator on the output field, the txid and input fields are not included in the projection. I believe that the correct behavior is exhibited by the $elemMatch operator, and should be the same with $slice.

            Assignee:
            nick.brewer Nick Brewer
            Reporter:
            lineus@gmail.com Kevin Huff
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: