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.