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

Array in input doc schema incorrectly affects $project output

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Query
    • Fully Compatible
    • ALL

      An array in an input doc can cause unexpected behavior when $project-ing a computed value within an output field of the same name.

      Test:

      c = db.c;
      
      // Note the $sort stages are required so the document source cursor includes 'a' in its Project.
      
      c.drop();
      c.save( { a:[ {}, {}, {} ], x:'bar' } );
      printjson( c.aggregate( { $sort:{ a:1 } }, { $project:{ a:{ c:'$x' } } } ).toArray() );
      
      c.drop();
      c.save( { a:[ 1, 2, 3 ], x:'bar' } );
      printjson( c.aggregate( { $sort:{ a:1 } }, { $project:{ a:{ c:'$x' } } } ).toArray() );
      

      Output

      Aaron-Staples-MacBook-Pro:mongo8 aaron$ ./mongo test.js
      MongoDB shell version: 2.2.0-rc1-pre-
      connecting to: test
      {
      	"result" : [
      		{
      			"_id" : ObjectId("501dc1a903c33b9898335417"),
      			"a" : [
      				{
      					"c" : "bar"
      				},
      				{
      					"c" : "bar"
      				},
      				{
      					"c" : "bar"
      				}
      			]
      		}
      	],
      	"ok" : 1
      }
      {
      	"result" : [
      		{
      			"_id" : ObjectId("501dc1a903c33b9898335418"),
      			"a" : [ ]
      		}
      	],
      	"ok" : 1
      }
      

      Expected output:

      Aaron-Staples-MacBook-Pro:mongo8 aaron$ ./mongo test.js
      MongoDB shell version: 2.2.0-rc1-pre-
      connecting to: test
      {
      	"result" : [
      		{
      			"_id" : ObjectId("501dc1d8aba874295f450cf6"),
      			"a" : {
      				"c" : "bar"
      			}
      		}
      	],
      	"ok" : 1
      }
      {
      	"result" : [
      		{
      			"_id" : ObjectId("501dc1d8aba874295f450cf7"),
      			"a" : {
      				"c" : "bar"
      			}
      		}
      	],
      	"ok" : 1
      }
      

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            aaron Aaron Staple
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: