Home > Net >  How to get most recent Date from array of objects?
How to get most recent Date from array of objects?

Time:09-15

I had array of objects in each object had startDate , endDate, ownnerID, ownerType.

I am getting the documents by uisng this below statement

var yesterdayDate = new Date();
yesterdayDate.setDate(yesterdayDate.getDate() - 1);
var next30daysDate = new Date();
next30daysDate.setDate(next30daysDate.getDate()   29);
const insurances = await Insurances.find({endDate: {$gte: yesterdayDate, $lte: next30daysDate}});



 [
  {
    docIds: [ 6285c7ce9fb2940007e61ce2 ],
    _id: 6285c7cd9fb2940007e61cde,
    startDate: 2022-03-31T18:30:00.000Z,
    endDate: 2022-09-28T18:30:00.000Z,
    ownerId: 61de54e8ae38620006457674,
    ownerType: 'vendor',
    createdAt: 2022-05-19T04:30:06.040Z,
    updatedAt: 2022-09-06T09:42:37.497Z,
    __v: 0,
    name: 'download'
  },
  {
    docIds: [ 631edc3da1c7060007242fca ],
    _id: 631edc3da1c7060007242fc2,
    startDate: 2022-09-13T18:30:00.000Z,
    endDate: 2022-09-27T18:30:00.000Z,
    ownerId: 6156aeeb152a3040b82fbc50,
    ownerType: 'vendor',
    createdAt: 2022-09-12T07:14:05.616Z,
    updatedAt: 2022-09-12T12:04:57.897Z,
    __v: 0,
    name: 'bid manager'
  },
  {
    docIds: [ 631f203b71f6f042243bcb26 ],
    _id: 631f203871f6f042243bcb1f,
    startDate: 2022-09-13T06:30:00.000Z,
    endDate: 2022-09-29T06:30:00.000Z,
    name: 'Javascript Training',
    ownerId: 6156aeeb152a3040b82fbc50,
    ownerType: 'vendor',
    createdAt: 2022-09-12T12:04:10.938Z,
    updatedAt: 2022-09-12T12:04:11.557Z,
    __v: 0
  },
  {
    docIds: [ 631f203b71f6f042243bcb28 ],
    _id: 631f203871f6f042243bcb20,
    startDate: 2022-09-12T18:30:00.000Z,
    endDate: 2022-09-27T18:30:00.000Z,
    name: 'SP-UserAccounts-200622-1021',
    ownerId: 6156aeeb152a3040b82fbc50,
    ownerType: 'vendor',
    createdAt: 2022-09-12T12:04:10.943Z,
    updatedAt: 2022-09-12T12:04:47.312Z,
    __v: 0
  },
  {
    docIds: [ 632155e5488a188c34ba5473 ],
    _id: 632155e1488a188c34ba546f,
    startDate: 2022-09-13T06:30:00.000Z,
    endDate: 2022-09-24T06:30:00.000Z,
    name: 'bid-project-details',
    ownerId: 6253c6a067a5b83c84855f99,
    ownerType: 'vendor',
    createdAt: 2022-09-14T04:17:41.082Z,
    updatedAt: 2022-09-14T04:17:41.674Z,
    __v: 0
  },
  {
    docIds: [ 6321fe4317dfea0007e828fe ],
    _id: 6321fe2217dfea0007e828c9,
    startDate: 2022-09-14T00:00:00.000Z,
    endDate: 2022-09-15T00:00:00.000Z,
    ownerId: 5bca41cbe5a5c1808da49b1c,
    ownerType: 'vendor',
    createdAt: 2022-09-14T16:15:30.986Z,
    updatedAt: 2022-09-14T16:16:03.604Z,
    __v: 0
  },
  {
    docIds: [ 6321ff3e17dfea0007e829dc ],
    _id: 6321fe2217dfea0007e828c6,
    startDate: 2022-09-15T00:00:00.000Z,
    endDate: 2022-09-15T00:00:00.000Z,
    ownerId: 5bca41cbe5a5c1808da49b1c,
    ownerType: 'vendor',
    createdAt: 2022-09-14T16:15:31.215Z,
    updatedAt: 2022-09-14T16:20:14.721Z,
    __v: 0
  },
  {
    docIds: [ 6321fef84377f600061f7f43 ],
    _id: 6321fe2217dfea0007e828c8,
    startDate: 2022-09-14T00:00:00.000Z,
    endDate: 2022-09-24T00:00:00.000Z,
    ownerId: 5bca41cbe5a5c1808da49b1c,
    ownerType: 'vendor',
    createdAt: 2022-09-14T16:15:31.233Z,
    updatedAt: 2022-09-14T16:19:04.341Z,
    __v: 0
  },
  {
    docIds: [ 6321ff5c17dfea0007e829f7 ],
    _id: 6321fe2217dfea0007e828c7,
    startDate: 2022-09-14T00:00:00.000Z,
    endDate: 2022-09-15T00:00:00.000Z,
    ownerId: 5bca41cbe5a5c1808da49b1c,
    ownerType: 'vendor',
    createdAt: 2022-09-14T16:15:31.298Z,
    updatedAt: 2022-09-14T16:20:44.631Z,
    __v: 0
  }
]

I am getting the results like this. Here my concern is one owner has different insurances going to expire on different dates.

Now I have to pick only one document with recent endDate by using the ownerId.

How can I solve this issue ?

CodePudding user response:

const insurances = await Insurances.find({ownerId: 'Your Owner Id', endDate: {$gte: yesterdayDate, $lte: next30daysDate}}).sort({endDate: -1}).limit(1);

The sort function will sort you arrays with new to old endDate, and limit will return only single document with latest endDate in an array.

CodePudding user response:

You want to take one value. Therefore, you can use findOne. With using sort(), we can achieve the data which has recent endDate.

  const insurances = await Insurances.findOne({$and: [{ownerId : mongoose.Types.ObjectId('OwnerId')}, {endDate: {$gte: yesterdayDate, $lte: next30daysDate}}]}).sort({endDate: -1})
  • Related