|
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
|
}
|
|