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

Lookup an array ob ObjectId doesn't guaranty the order

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: 3.4.4
    • Fix Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Operating System:
      ALL

      Description

      I have 2 collections:

      • Foo (_id, bars[])
      • Bar (_id)

      `bars` is the array of `Bar`'s ObjectId.

      I would like to use `lookup` to get the array of `Bar` in EXACTLY order that the Id stored in `bars`.

      {
      	$lookup: {
      		from: 'bars',
      		localField: 'bars,
      		foreignField: '_id',
      		as: 'bars'
      	}
      }
      

      Everything are ok, UNTIL I $push another Bar's ObjectId to BEGIN of the `bars` array. The $lookup will returns the array which the new one is in the end of array, NOT begin of array as my expectation.

      For example:

      {
      	_id: ...,
      	bars: [A_id, B_id]
      }
      

      => A, B
      But, after $push C_id to BEGIN of array:

      {
      	_id: ...,
      	bars: [C_id, A_id, B_id]
      }
      

      => A, B, C
      And so on for D_id => A, B, C, D

      My expectation is: A, B, C, D.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: