I'm currently facing the challenge of sorting a collection based on a specific value while another value is equal to something else. Please refer to the document below:
{
"_id": "zLFp8KptxzACGtAZj",
"createdAt": "2015-05-28T21:11:57.044Z",
...
"profile": {
"firstname": "Nelle",
"lastname": "Carroll",
...
"services": [
{
"serviceId": "Gy7uptfk8LNWMgEQy",
"rate": 19
},
{
"serviceId": "KvFETNLK8cJ78e6gy",
"rate": 1
},
{
"serviceId": "xwY532yxcWQ7qAjuP",
"rate": 42
}
],
}
...
}
Let's say I have a collection of around 10 users, and I want to sort them by rate for those whose services include the serviceId xwY532yxcWQ7qAjuP
. I can locate them using
Meteor.users.find({ 'profile.services.serviceId' : service });
. But if I wish to order the users by their rates using Meteor.users.find({ 'profile.services.serviceId' : service }, , { sort: { 'profile.services.rate' : 1 } });
, it will arrange them based on the highest rate across all services, not just for xwY532yxcWQ7qAjuP
. How can I achieve sorting by rate where serviceId equals xwY532yxcWQ7qAjuP
?