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

Lookup an array ob ObjectId doesn't guaranty the order

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.4.4
    • Component/s: Aggregation Framework
    • Labels:
      None
    • ALL

      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.

            Assignee:
            mark.agarunov Mark Agarunov
            Reporter:
            binh.cao CAO Quang Binh
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: