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

Aggregation Pipeline: $unwind always gives an empty result with nested fields

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Blocker - P1 Blocker - P1
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • ALL
    • Hide

      Given the following documents:
       
       

      { addresses: [{ city: 'Chicago' }, { city: 'London' }] }
      { addresses: [{ city: 'Seattle' }] }
      { addresses: [{ city: 'Chicago' }, { city: 'Seattle' }] }

       
       
      And then I call $unwind in an aggregation pipeline:
       
       

      { "$unwind": "$addresses.city" }
      { "$group": { _id: "$addresses.city", counts: { "$sum": 1 } } }
      
      => {}

       
      It will yield an empty result.
       
      As a workaround, if I add a $project it will work:
       

      { "$project": { "p" => "$addresses.city" } }
      { "$unwind": "$p" }
      { "$group": { _id: "$p", counts: { "$sum": 1 } } }
      
      =>
      { _id: "Chicago", counts: 2 }
      { _id: "London", counts: 1 }
      { _id: "Seattle", counts: 2 }

       
       

      Show
      Given the following documents:     { addresses: [{ city: 'Chicago' }, { city: 'London' }] } { addresses: [{ city: 'Seattle' }] } { addresses: [{ city: 'Chicago' }, { city: 'Seattle' }] }     And then I call $unwind in an aggregation pipeline:     { "$unwind" : "$addresses.city" } { "$group" : { _id: "$addresses.city" , counts: { "$sum" : 1 } } } => {}   It will yield an empty result.   As a workaround, if I add a $project it will work:   { "$project" : { "p" => "$addresses.city" } } { "$unwind" : "$p" } { "$group" : { _id: "$p" , counts: { "$sum" : 1 } } } => { _id: "Chicago" , counts: 2 } { _id: "London" , counts: 1 } { _id: "Seattle" , counts: 2 }    

      When using the AggregationPipeline, $unwind always gives an empty result with nested fields
       
      For example:

      { "$unwind": "$addresses.city" }

      Adding a $project stage will fix the issue:

      { "$project": { "p" => "$addresses.city" } }
      { "$unwind": "$p" }

      Refer to Steps to Reproduce below for details.

            Assignee:
            Unassigned Unassigned
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: