Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4916

Mongoid aggregation not preserving order of embedded documents

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.0.5
    • Component/s: Associations, Query
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Let's say you have a User document, and embedded inside of it a posts document order by title:

      ```
      embeds_many :posts, order: :title.asc
      ```

      If you query a user's post: `User.first.posts`, the posts are returned in proper order. 

       

      However, if you use an aggregation $match:

      ```
      User.collection.aggregate([ 

      { '$match' => \{ id: 1 }

      } ])
      ```

      The posts are returned in a different order than by title. To order the posts again you have to use the aggregation pipeline:

      ```

      { "$unwind" => "$posts" }

      , {
      "$sort" =>

      { "posts.title": 1 }

      },
       
      ```

       

      Why is the aggregation pipeline not preserving the order of embedded documents??

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            ibrah1440@gmail.com Ibraheem Ahmed
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: