Mongoose how to sort by date which is in array of objects of a field

mahdi

i want to grab data sorted by its date but the date is not in a field's value, its in the field's value's object's array(if i said it correctly).

here is an example of the data i have:

{
  role: "User",
  fullName: "Verna Pagac",
  username: "dwightkoss95",
  email: "[email protected]",
  orders: [{
    buyerUsername: 'admin',
    boughtAt: 2022-09-20T20:14:59.304Z
  },
  {
    buyerUsername: 'admin',
    boughtAt: 2022-10-30T22:35:35.546Z
  }]
}

after i extracted the orders by the following command, i want to sort them by the boughtAt but how?

const usersWithOrders = await users
        .find({
            orders: { $exists: true, $ne: [] }
        })

const orders = []
usersWithOrders.map((user) => {
        for (i=0 ; i<user.orders.length ; i++) {
            orders.push(user.orders[i])
        }
    })

i want the newer order to be top.

console.log(orders)

// now it shows the following out put:

/*
[{
    buyerUsername: 'admin',
    boughtAt: 2022-09-20T20:14:59.304Z
  },
  {
    buyerUsername: 'admin',
    boughtAt: 2022-10-30T22:35:35.546Z
  }]
*/
Maku

If you want you can sort directly from db using the aggregation framework

db.users.aggregate([
  { $unwind: '$orders' },
  { $sort: { 'orders.boughtAt': -1 } },
  {
    $group: {
      _id: '$_id',
      user: { $first: '$$ROOT' },
      orders: { $push: '$orders' },
    },
  },
])

or if you prefer you can sort in JS

orders.sort((a, b) => b.boughtAt - a.boughtAt)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to add more fields(in mongoose schema) into a field(which is an array of objects) and these objects are reference to another mongoose schema?

Sort array of objects with date field by date

How to specify field which is array of objects, objects of array has one of attribute as unique but optional in Mongoose?

How to sort an array of objects by date?

How to sort an array by date field descending order?

how to sort json array by date field

How to sort an array of objects based on date

How to sort an array of objects by the date that are in string form?

Angular how to sort date objects of array?

How to sort an array of objects by multiple date fields?

How to sort nested array of objects by date

How to sort array of objects have date properties?

Sort Objects in Array by date

How to add a virtual field to mongoose array of objects in a schema?

Mongoose - How to query field in the last object of an array of objects

Mongoose How to sort .populate field

How can I sort an array full of objects by the date (which is represented by index 0-23 in the key attribute of each element)?

Sort array of objects by field and then alphabetically

Sort Array of objects according to date

How do I sort an array of objects by a particular field?

How to 'sort' two objects based on number of occurrences of elements in an array field?

How to map data of a field in an JSON object which is an array of objects

How to sort a string array which includes a date in c#?

Mongoose Filter & Sort Array of Objects Using Aggregate

How to sort the date form to latest to the oldest in array of objects react js

How to sort multiple class objects with date property in single array with swift?

How to sort array of objects based on descending order of date in reactjs?

How to get the objects inside an array of objects which is inside an document in mongoose/mongodb?

Sort python array field as date string by date