2020-05-13

MongoDB - Get the object with the latest date (from an array of objects)

db.collection_name.aggregate([    
  {$project: {


    "output_field_name": {


      $arrayElemAt: [
        "$array_of_objects",
        {
          "$indexOfArray": [
            "$array_of_objects.date_name",
            { "$max": "$array_of_objects.date_name" }
          ]
        }
      ]


    }


  }}  
]);

No comments:

Post a Comment