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

Expand $unwind behavior to include empty documents optionally

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.1.8
    • 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.

            Assignee:
            charlie.swanson@mongodb.com Charlie Swanson
            Reporter:
            rareddy9 Ramesh Reddy
            Votes:
            3 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: