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

Expand $unwind behavior to include empty documents optionally

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.1.8
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Backwards Compatibility:
      Fully Compatible

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: