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

$lookup should have option not merge duplicate array item in result (not auto unique)

    • Type: Icon: Improvement Improvement
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None
    • Query
    • Fully Compatible

      Think below $lookup aggregate:

      use test
      db.test.insert([{id:1, x:10}, {id:2, y:11}])
      db.test.insert({id:3, a:[{id:1}, {id:2}, {id:1}]})
      db.test.aggregate([ 
        {$match:{id:3}}, 
        {$lookup: {from:'test', localField:'a.id', foreignField:'id', as:'xyz'}},  
        {$project:{xyz:1}}
      ])
      

      The result have merged result with array length=2, but the source array a has length=3

      {"xyz" : [ { "id" : 1, "x" : 10}, { "id" : 2, "y" : 11} ] }
      

      The above result can cause issue in client application since if the id is same, the result will merged, different length from source, but if all the source id is unique, they have identical length.

      The problem here is, it's related to input data, the application cannot predict whether the result is merged, and the 1-1 relation from source array to result array is broken!

      Maybe can add an option to $lookup stage object say "unique: false" ? And not merge the result automatically.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            futurist Jame Yang
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: