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

Projection with ExpressionObject does not round trip (serialize + parse)

    • Query Optimization
    • Fully Compatible
    • ALL
    • QO 2023-08-07, QO 2023-08-21, QO 2023-09-04, QO 2023-09-18

      When we serialize a projection, as in {$set: {a: _}}, we need to be careful when the expression happens to be an object-expression, such as {b: "$_id"}. Currently, serialize() incorrectly produces {$set: {a: {b: "$_id"}}} which has a different meaning.


      For example, set up a collection like this:

      db.c.drop();
      db.c.insert({_id: 123, a: [{z:99}, {z:100}]});
      

      Run this query:

      db.c.aggregate([ {$set: {a: {$ifNull: [null, {b: "$_id"}]}}} ])
      

      On a non-sharded collection, this correctly returns docs where the entire field 'a' is replaced with a new document {b: 123}:

      { "_id" : 123, "a" : { "b" : 123 } }
      

      However, if you set up the same example on a sharded collection:

      $ mongo --nodb
      > st = new ShardingTest({nodes:2, s:1})
      
      $ mongo --port $(pgrep mongos)
      mongos> db.c.drop();
      mongos> db.c.insert({_id: 123, a: [{z:99}, {z:100}]});
      mongos> db.c.createIndex({_id: 'hashed'})
      mongos> sh.shardCollection('test.c', {_id: 'hashed'})
      

      And you run the same query, you get a wrong answer:

      mongos> db.c.aggregate([ {$set: {a: {$ifNull: [null, {b: "$_id"}]}}} ])
      { "_id" : 123, "a" : [ { "z" : 99, "b" : 123 }, { "z" : 100, "b" : 123 } ] }
      

      Instead of replacing all of 'a', we have added a field 'b' to every array element, and preserved other subfields.


      This happens because internally, after optimization, we end up with an AST that we cannot correctly serialize. We have something like (ProjectionNode "a" (ExpressionObject "b" ...)) which we serialize as {$set: {a: {b: _}}}, as is then misinterpreted as (ProjectionNode "a" (ProjectionNode "b" ...)).

            Assignee:
            matt.olma@mongodb.com Matt Olma
            Reporter:
            david.percy@mongodb.com David Percy
            Votes:
            0 Vote for this issue
            Watchers:
            15 Start watching this issue

              Created:
              Updated:
              Resolved: