[SERVER-6714] Array in input doc schema incorrectly affects $project output Created: 05/Aug/12  Updated: 06/Dec/22  Resolved: 24/Jun/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Aaron Staple Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-18966 Allow exclusion in $project stage of ... Closed
Assigned Teams:
Query
Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

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
}



 Comments   
Comment by Asya Kamsky [ 25/Jan/16 ]

I can't quite put my finger on why, but it seems to be like this is behaving somewhat correctly...

Comment by Aaron Staple [ 05/Aug/12 ]

Tentatively putting in 2.3.0.

Generated at Thu Feb 08 03:12:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.