Reputation: 584
I am trying to figure out how you would sort a collection by value where another value is == to something else see 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
}
],
}
...
}
Say I have a collection of about 10 users whose services contain the serviceId xwY532yxcWQ7qAjuP
I could find them using Meteor.users.find({ 'profile.services.serviceId' : service });
but if I wanted to sort the users by the their rate using Meteor.users.find({ 'profile.services.serviceId' : service }, , { sort: { 'profile.services.rate' : 1 } });
It sorts the users by who has the highest rate in any service not just xwY532yxcWQ7qAjuP
So how would I go about sorting by rate where serviceId === xwY532yxcWQ7qAjuP
?
Upvotes: 2
Views: 592
Reputation: 4820
Sadly as far as I know, this is not really possible natively on MongoDB. You can probably make it work using the agreggate framework:
meteor add meteorhacks:aggregate
Then:
Meteor.users.aggregate([
{$unwind:"$profile"},
{$unwind:"$profile.services"},
{$match:{"profile.services.serviceId":service}},
{$sort:{"profile.services.rate":1}}
]);
Upvotes: 1