-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
Minor Change
-
ALL
-
Query 2019-12-02, Query 2019-12-16, Query 2019-12-30
The following example demonstrates an inconsistency within the $project stage:
> db.foo.insert({a: [1, {b: 2}, 3, {}]}) // Note 4 elements in 'a'. WriteResult({ "nInserted" : 1 }) > db.foo.aggregate([{$project: {"a.b": {$literal: "NEW"}}}]).pretty() { "_id" : ObjectId("57d6d3cd150e60d4d52d9714"), "a" : [ // Note there are still 4 elements in 'a'. scalar values have been replaced with documents. { "b" : "NEW" }, { "b" : "NEW" }, { "b" : "NEW" }, { "b" : "NEW" } ] } > db.foo.aggregate([{$project: {"a.b.c": {$literal: "NEW"}}}]).pretty() { "_id" : ObjectId("57d6d3cd150e60d4d52d9714"), "a" : [ // Note there are only 2 values in 'a'. { "b" : { "c" : "NEW" } }, { "b" : { "c" : "NEW" } } ] }
This happens because the second projection adds 'a.b' to its dependencies instead of just 'a', which loses the 'shape' of 'a':
> db.foo.explain().aggregate([{$project: {"a.b.c": {$literal: "NEW"}}}]) { "stages" : [ { "$cursor" : { "query" : { }, "fields" : { "a.b" : 1, "_id" : 1 }, ... // Other explain info. } > db.foo.find({}, {_id: 1, "a.b": 1}).pretty() { "_id" : ObjectId("57d6d443f5f2b41fe6bb267c"), "a" : [ // Note only 2 of 4 elements result from this projection. { "b" : 2 }, { } ] }
- is related to
-
SERVER-30812 When using an array element as the local field for $lookup, $project doesn't work
- Closed