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

Documents sorted in reverse order

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.0.2
    • Component/s: Aggregation Framework
    • None
    • ALL
    • Hide
      db.test.save({data:[{f:1,v:'Adam'}]});
      db.test.save({data:[{f:1,v:'Bob'}]});
      db.test.save({data:[{f:1,v:'Charles'}]});
      
      db.test.aggregate([
        { $match: { 'data.f': 1 } },
        { $unwind: '$data' },
        { $sort: { 'data.v': 1 } },
        { $group: {
            _id: '$_id',
            data: {
              $push: { f: '$data.f', v: '$data.v' }
            }
          }
        }
      ]);
      
      Show
      db.test.save({data:[{f:1,v: 'Adam' }]}); db.test.save({data:[{f:1,v: 'Bob' }]}); db.test.save({data:[{f:1,v: 'Charles' }]}); db.test.aggregate([ { $match: { 'data.f' : 1 } }, { $unwind: '$data' }, { $sort: { 'data.v' : 1 } }, { $group: { _id: '$_id' , data: { $push: { f: '$data.f' , v: '$data.v' } } } } ]);

      The code below produces this:

      { "_id" : ObjectId("5537436ea93a16b4e1d4ad17"), "data" : [ { "f" : 1, "v" : "Charles" } ] }
      { "_id" : ObjectId("5537436ea93a16b4e1d4ad16"), "data" : [ { "f" : 1, "v" : "Bob" } ] }
      { "_id" : ObjectId("5537436ea93a16b4e1d4ad15"), "data" : [ { "f" : 1, "v" : "Adam" } ] }
      

      But it seems to me it should be the reverse. Changing the 1 to -1 in the $sort seems to reverse it again so it's in alphabetical order.

      Oddly, the phenomenon is not always consistent – it seems to be consistent when run via the console, but when I run the equivalent code via either PHP or Ruby, the records are sometimes ordered CAB or ACB etc.

      Tested on both WT and MMAPv1.

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            matt.parlane@gmail.com Matt Parlane
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: