-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
None
-
Fully Compatible
Currently when a document has nested array based documents, $unwind only shows records where parent documents contain the $unwind field. It would be useful in certain scenarios to have an $unwind stage with some other optional configuration, which will produce an output document for a document that does not contain the $unwind field.
In a collection with the following documents:
{ "FirstName" : "John", "LastName" : "Doe", "Notes" : [ { "CustomerId" : DBRef("Customer", 1002), "PostDate" : ISODate("2014-02-05T00:04:45Z"), "Comment" : "Rocks" }, { "CustomerId" : DBRef("Customer", 1002), "PostDate" : ISODate("2014-02-05T04:06:45Z"), "Comment" : "Sucks" } ], "_id" : 1002 } { "FirstName" : "Jane", "LastName" : "Smith", "_id" : 1001 }
When you run this pipeline:
db.coll.aggregate([{$unwind: '$Notes'}])
You get the following results:
{ "FirstName" : "John", "LastName" : "Doe", "Notes" : { "CustomerId" : DBRef("Customer", 1002), "PostDate" : ISODate("2014-02-05T00:04:45Z"), "Comment" : "Rocks" }, "_id" : 1002 } { "FirstName" : "John", "LastName" : "Doe", "Notes" : { "CustomerId" : DBRef("Customer", 1002), "PostDate" : ISODate("2014-02-05T04:06:45Z"), "Comment" : "Sucks" } ], "_id" : 1002 }
It would be nice if it also returned the following result:
{ "_id" : 1001, "FirstName" : "Jane", "LastName" : "Smith" }
Here is user groups discussion on this topic
https://groups.google.com/forum/#!topic/mongodb-user/UKYPddRN37E
There is workaround exists using the $IFNULL, see the above link.
- is depended on by
-
SERVER-19095 $lookup
- Closed
- related to
-
SERVER-8088 $unwind of non-array should be allowed
- Closed
-
SERVER-20168 Add an option to $unwind to include documents with empty arrays
- Closed