// Populate : var testcol = db.getCollection('Test1'); for (var i = 0; i < 100000; i++) { var date = new Date((1496211798264 + i)); var item = { subject: "Test " + i, events: [], update: date }; for (var j = 0; j < 15; j++) { item.events.push({ event: "XXXX" + j, date: new Date(date-j), }); } testcol.insert(item); } // Indexes index : { "events.0.date" : 1 } index : { "update" : 1 } // Sort "events.0.date" with "_id" testcol.aggregate([ { $sort:{ "events.0.date": -1, '_id':1 } }, { $limit:4 }, { $project:{ "subject":true, "update": { "$arrayElemAt": ["$events.date", 0] } } } ]) //wrong result /* 1 */ { "_id" : ObjectId("592e7d182fc987735f3dda4d"), "subject" : "Test 0", "update" : ISODate("2017-05-31T10:23:18.264+04:00") } /* 2 */ { "_id" : ObjectId("592e7d182fc987735f3dda4e"), "subject" : "Test 1", "update" : ISODate("2017-05-31T10:23:18.265+04:00") } /* 3 */ { "_id" : ObjectId("592e7d182fc987735f3dda4f"), "subject" : "Test 2", "update" : ISODate("2017-05-31T10:23:18.266+04:00") } /* 4 */ { "_id" : ObjectId("592e7d182fc987735f3dda50"), "subject" : "Test 3", "update" : ISODate("2017-05-31T10:23:18.267+04:00") } // Sort "events.0.date" with "_id" allow disk testcol.aggregate([ { $sort:{ "events.0.date": -1, '_id':1 } }, { $limit:4 }, { $project:{ "subject":true, "update": { "$arrayElemAt": ["$events.date", 0] } } } ], { allowDiskUse: true }) //wrong result /* 1 */ { "_id" : ObjectId("592e7d182fc987735f3dda4d"), "subject" : "Test 0", "update" : ISODate("2017-05-31T10:23:18.264+04:00") } /* 2 */ { "_id" : ObjectId("592e7d182fc987735f3dda4e"), "subject" : "Test 1", "update" : ISODate("2017-05-31T10:23:18.265+04:00") } /* 3 */ { "_id" : ObjectId("592e7d182fc987735f3dda4f"), "subject" : "Test 2", "update" : ISODate("2017-05-31T10:23:18.266+04:00") } /* 4 */ { "_id" : ObjectId("592e7d182fc987735f3dda50"), "subject" : "Test 3", "update" : ISODate("2017-05-31T10:23:18.267+04:00") } // Sort "update" (same as "events.0.date") with "_id" testcol.aggregate([ { $sort:{ "update": -1, '_id':1 } }, { $limit:4 }, { $project:{ "subject":true, "update": { "$arrayElemAt": ["$events.date", 0] } } } ]) //Good result /* 1 */ { "_id" : ObjectId("592e80b52fc987735f4d1c8c"), "subject" : "Test 999999", "update" : ISODate("2017-05-31T10:39:58.263+04:00") } /* 2 */ { "_id" : ObjectId("592e80b52fc987735f4d1c8b"), "subject" : "Test 999998", "update" : ISODate("2017-05-31T10:39:58.262+04:00") } /* 3 */ { "_id" : ObjectId("592e80b52fc987735f4d1c8a"), "subject" : "Test 999997", "update" : ISODate("2017-05-31T10:39:58.261+04:00") } /* 4 */ { "_id" : ObjectId("592e80b52fc987735f4d1c89"), "subject" : "Test 999996", "update" : ISODate("2017-05-31T10:39:58.260+04:00") }