sort by date with aggregate request in mongodb

aggregation-framework, mongodb

Solution

Your aggregate query is incorrect. You add the sort and limit to the match, but that's now how you do that. You use different pipeline operators:

db.friends.aggregate( [
    { $match: { advertiser: "noc3" } }, 
    { $sort: { createdDate: -1 } },
    { $limit: 1 },

Your other pipeline operators are bit strange too, and your code vs query mismatches on `timestamps` vs `createdDate`. If you add the expected output, I can update the answer to include the last bits of the query too.

Problem

I would like to retrieve a list of values ​​that comes from the oldest document currently signed.But i failed to select a document absed on the date.Thanks here is json : ``` "ad" : "noc3", "createdDate" : ISODate(), "list" : [ { "id" : "p45", "value" : 21, }, { "id" : "p6", "value" : 20, }, { "id" : "4578", "value" : 319 } ] ``` and here my aggregate request : ``` db.friends.aggregate({$match:{advertiser:"noc3", {$sort:{timestamps:-1},{$limit:1} }},{$unwind:"$list"},{$project:{_id: "$list.id", value:{$add:[0]}}}); ```

Original source