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

94
Views
Sorting deeply nested array by number key value pair in MongoDB aggregation pipeline

I have the following schema for products in my website

[{
    product_code: String,
    title: String,
    description: String,
    ....
    ....
    product_variants: [{ type: new mongoose.Schema(
        {
        variant_code: String,
        variant_title: String,
        slug: String,
        ...
        ...
        variant_details: [{ type: new mongoose.Schema(
            {
                size: String,
                local_price: { type: Number, default: 0 },
                local_discount: { type: Number, default: 0 },
                ...
                ...
                inventory: [{
                    type: new mongoose.Schema(
                        {
                            branch_id: mongoose.Schema.Types.ObjectId, quantity: Number, vendor_price: { type: Number, default: 0 }
                        }
                    )
                }]
            }
        )
        }]
        }
    ) 
    }]
}]

To display the product details, I need to sort the inventory array by lowest vendor price and greatest quantity. Since the array is deeply nested, I'm having trouble in figuring out how to build the aggregation pipeline and I'm in need of help.

Can someone help me with writing the aggregation pipeline for this condition? Thanks.

about 4 years ago · Juan Pablo Isaza
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!