Assuming we have the following documents in our collection:
{
name: 'Product1',
prices: [
{ shop: 'shop1', price: 1 },
{ shop: 'shop2', price: 2 }
]
},
{
name: 'Product2',
prices: [
{ shop: 'shop3', price: 5 },
{ shop: 'shop4', price: 3 }
]
}
I need to get the following results from these documents using mongodb:
{
name: 'Product1',
bestPrice: { shop: 'shop1', price: 1 }
},
{
name: 'Product2',
bestPrice: { shop: 'shop4', price: 3 }
}
In other words, I want to get documents with their lowest prices ordered by value of the price field. How could I do it using using mongodb aggregation framework?