Meteor DB API needs a mapreduce or projection feature to display of collections w/ subarrays that match userId
I can't see a clean way to do this w/o something like MongoDB's mapreduce or projection features which is not in Meteor's collection API.
E.g., you easily can't take the Parties Meteor example and properly show "all parties I'm invited to and my rsvp status for each" on a single page like this: party1 myrsvp is yes party2 myrsvp is no party 3 myrsvp is maybe
You can get a collection of parties that you're invited to, but the document collection includes all the rsvps that have been put in, so you can't do a Handlebars #each that shows the party name, and your rsvp status w/o hacking around this in one of two ways: a) write a Handlebars helper that iterates through the rsvp array to look for one that matches your user ID (but you'll be wasting network bandwidth sending all the rsvps down and if the rsvps are supposed to be private, this is a security issue) b) write a custom published collection on the server that recreates and array of documents that reduces down the rsvp field w/ the rsvp that matches your user ID (basically hacking a mapreduce function in each time you do this) but at least this cuts down network bandwidth
I think (b) is the only viable option for now but it'd be nice if something like mongo's mapreduce or projections were added instead so you don't have to write custom collection handlers each time you need something like this...
On the server, anything you pass to the fields option to find is passed directly to Mongo as a projection operator, so you can use (eg) the $elemMatch projection operator. So, for example:
Meteor.publish("my-rsvps", function () {
return Parties.find(
{$or: [{"public": true}, {invited: this.userId}, {owner: this.userId}]},
{fields: {rsvps: {$elemMatch: {user: this.userId}}}}
);
});
will publish only my RSVPs of the parties that I can see (you'll probably want to also edit the main "parties" subscription to have a {fields: {rsvps: 0}}).
Besides the $or syntax, you might get some milage out of the $in syntax.
http://docs.mongodb.org/manual/reference/operator/in/
@awatson1978 Not sure what you're getting at here? (The $or part is just verbatim from existing code; it's the fields part that's new.)
:shrug: I've been getting some success using clauses like the following.
Meteor.publish("my-rsvps", function () {
return Parties.find( {fields: {rsvps: {user: $in: [this.userId] } } } );
});
$in works just like $or, but you can be a little more concise sometimes. It sort of swaps the subject and object of the verb. Slightly different syntax, basically the same result.
Sure, but what I'm taking about here is the fields (projection) clause, not the query. I don't think $in works in projections, does it?
Thanks, David. $elemMatch is what I wanted from MongoDB. It's not documented in the official operator list in the Meteor docs or examples, so I assumed it wasn't officially supported since you guys were trying to make the API DB agnostic. I had googled this quite a bit and there are a few stackoverflow comments that projections and mapreduce were not supported in meteor. Guess I should have just tried it first :-)
Abigail: FYI, doing $in actually isn't needed. You can do {rsvsp: this.userId} and that's an implicit $in. The problem is the doc returned will return all the RSVPs in that subarray instead of only your RSVP. If you use the $elemMatch operator, it'll just return your RSVP.
I actually added a pretty ugly workaround yesterday...added another collection named rsvps with the "user to rsvp" mapping (filtered by user) and then sent this down to the client, then added a Handlebars helper to look up the RSVP value in the #each loop to display the party. Works fine, but yuck :-P
We haven't implemented the $elemMatch projection operator in minimongo, so it doesn't work on the client, just on the server (where queries are mostly just processed by MongoDB).
In the future, we might use some Minimongo code on the server to help optimize live query processing, which means that forms unsupported by Minimongo might not get the full benefits of the optimizations. But I think we would keep them working, at least.
@glasser, I don't know am I right, but minimongo supports $elemMatch, and I was using it while 5.5 problems with arrays match.
Minimongo supports the query operator $elemMatch, not the projection operator $elemMatch :)
I want to get a list of each of the unique field value from a collection
e.g.
CityCollection.find({state: 'Illinois'}) // { city: 'Chicago', state: 'Illinois'}, {city: 'champagne', state: 'Illinois'}, ...
ListOfStates = CityCollection.??? // ['Illinois', 'Arizona', ...]
It seems like a CityCollection.reduce function would be real handy. or maybe i'm obtuse to something preferred.
Check out https://github.com/jhoxray/meteor-mongo-extensions (mrt add mongodb-aggregation) - it implements server side distinct(), aggregate() and mapReduce() mongo methods on the server side (you can call client side, but it simply directs the call to the server). We use it in our BI app.
For your task, distinct() should work.
the fields thing could be a bit better documented :\ had some trouble with that one until I found that issue, as it seemed to work in meteor mongo but not meteor itself
:disappointed_relieved: AlexFrazer +1
@glasser I hope what you said here was actually put in the Meteor docs, would've saved me a lot of time. I was going around in circles about SO and meteor docs since the past two days.
On the server, anything you pass to the fields option to find is passed directly to Mongo as a projection operator, so you can use (eg) the $elemMatch projection operator. So, for example: ...
We haven't implemented the $elemMatch projection operator in minimongo, so it doesn't work on the client, just on the server (where queries are mostly just processed by MongoDB).
PS: Anyway, I like you guys and what you're doing with meteor :)
@glasser Also, projection operator elemMatch works very differently to the way mongodb uses it. I'm using it in a publish function like shown below and the entire query turns up empty when there's no array element with the given currentUserId. MongoDB however just removes the particular field active_bets if the element was not found. Is there a way I can modify the query to get an empty active_bets if the element was not found?
Games_collection.find(
{
table_id : currentTableId,
"active_bets.user_id": currentUserId
},
{
fields: {
limit : 1,
table_id : 1,
message : 1,
timer_value: 1,
joker : 1,
andar_cards: 1,
bahar_cards: 1,
active_bets: {$elemMatch: {user_id: currentUserId}},
game_result: 1,
createdAt : 1,
updatedAt : 1
},
sort : {createdAt: -1}
});
Here's a document from Games collection for reference:
{
"_id": "55d9ae3f230b5da123c12aae",
"table_id": "5y8iyYk4ZPvCYbimv",
"message": "asd1",
"timer_value": 0,
"joker": "sa",
"andar_cards": [
"s2",
"s4"
],
"bahar_cards": [
"s3",
"s5",
"s6",
"c4"
],
"active_bets": [
{
"user_id": "eK9SCBuEEG6tnxjAF",
"round_1_Andar": 2000,
"round_1_Bahar": 3866,
"round_2_Andar": 3196,
"round_2_Bahar": 4774
}
],
"game_result": "",
"createdAt": "2015-08-20T10:12:08.000Z",
"updatedAt": "2015-07-20T10:12:08.000Z"
}
@glasser Nevermind, silly me. :relieved: I'd left the "active_bets.user_id": currentUserId in the query portion. It is as to be expected.