Reversing the display order with nodejs

Asked

Viewed 37 times

1

Good? I would like to know how to reverse the order of display of bank items

async index(req, res){
    const docs = await Vag.find();
    return res.json(docs);
},

[
 {item1},
 {item2},
 {item3}
]

If so the display order is increasing, as I do for the data to be displayed decreasingly.

ps: I am using Mongodb

1 answer

2


For example

db.orders.find().sort( { amount: -1 } )

using the -1 the order is decreasing "down"

{ "_id" : 2, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 50 }
{ "_id" : 4, "item" : { "category" : "cake", "type" : "lemon" }, "amount" : 30 }
{ "_id" : 5, "item" : { "category" : "cake", "type" : "carrot" }, "amount" : 20 }
{ "_id" : 3, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 15 }
{ "_id" : 1, "item" : { "category" : "cake", "type" : "chiffon" }, "amount" : 10 }
{ "_id" : 6, "item" : { "category" : "brownies", "type" : "blondie" }, "amount" : 10 }
  • It worked here, thank you very much Mauro Rocha.

  • 1

    Cool! Good luck there!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.