Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
How populate items to Model in order

I have a question.

How do I sort the items which I add to the Model via .populate by some variable in the data?

The data I get:

"items": [
                {
                    "_id": "xxxx",
                    "name": "Item 1",
                    "price": 4000,
                    "order": 2,
                    "user": {
                        "_id": "xxxx",
                    }
                },
                {
                    "_id": "xxxx",
                    "name": "Item 2",
                    "price": 4000,
                    "order": 1,
                    "user": {
                        "_id": "xxxx",
                    }
                }
            ],

Here I want them to be sorted by the order variable. So Item 1 will be second and Item 2 will be first.

The order variable is not required, so if item does not have order, it should be sorted on last position.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

db.collection.aggregate({
"$sort": {order: 1}
})

you can use -1 for descending order. for live test check below link

mongodb sort result

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!