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)

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • None
    • Aggregation Framework
    • None
    • Query
    • Fully Compatible

    Description

      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.

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: