I want to sort the following generalized result using the aggregation $ value of the sort price closest to 92 The
aggregation I used before
db.units.aggregate([
{$match: {category: 'a'}},
{$limit: 3},
{$project: {price:1, name: 1, category: 1}}
]);
Output
[{'_id': '111', 'price': 100, 'name': 'abc', 'category': 'a'}
{'_id': '222', 'price': 90, 'name': 'efg', 'category': 'a'}
{'_id': '333', 'price': 80, 'name': 'xyz', 'category': 'a'}]
Output Required:
[{'_id': '222', 'price': 90, 'name': 'efg', 'category': 'a'}
{'_id': '111', 'price': 100, 'name': 'abc', 'category': 'a'}
{'_id': '333', 'price': 80, 'name': 'xyz', 'category': 'a'}]
Note. Price field is an embedded object.
source
share