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

$addToSet changes the sequence while $push maintains array sequence

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 4.2.11
    • Component/s: Aggregation Framework
    • Labels:
      None
    • ALL
    • Hide

      Here's the following example.

      unwind

      { path: '$data', includeArrayIndex: 'index', preserveNullAndEmptyArrays: true }

      sort

      { "_id": 1 }

      where _id are sequence of numbers 1,2,3,4 etc

      group

      {
      _id:"$id",  //$id is objectid
      title:

      { $first: '$title' }

      ,
      description:

      { $first: '$description' }

      ,
      episode:

      { $first:'$episode' }

      ,
      seasons:

      { $first: '$seasons' }

      ,
      episodes:

      { $first:"$episodes" }

      ,
      dur:

      { $first: '$dur' }

      ,
      "4k":

      { $first: '$4k' }

      ,
      hlghdr:

      { $first: '$hlghdr' }

      ,
      progress:{$first:null},
      image:

      { $first:"$image" }

      ,
      watchlist:

      { $first:"$watchlist" }

      ,
      type:

      { $first: '$type' }

      ,

      data:

      { $push: '$data'  /// <- here the sequence gets changed when using addToSet }

      }

       

      Show
      Here's the following example. unwind { path: '$data', includeArrayIndex: 'index', preserveNullAndEmptyArrays: true } sort { "_id": 1 } where _id are sequence of numbers 1,2,3,4 etc group { _id:"$id",  //$id is objectid title: { $first: '$title' } , description: { $first: '$description' } , episode: { $first:'$episode' } , seasons: { $first: '$seasons' } , episodes: { $first:"$episodes" } , dur: { $first: '$dur' } , "4k": { $first: '$4k' } , hlghdr: { $first: '$hlghdr' } , progress:{$first:null}, image: { $first:"$image" } , watchlist: { $first:"$watchlist" } , type: { $first: '$type' } , data: { $push: '$data'  /// <- here the sequence gets changed when using addToSet } }  

      While using group function of aggregation, the $addToSet for an array randomizes the sequence.

      Where as $push keeps the sequence as it is.

       

            Assignee:
            edwin.zhou@mongodb.com Edwin Zhou
            Reporter:
            sudarshan.chavan@travelxp.tv Sudarshan Chavan
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: