Why i can't use the $lookup operator in meteor 1.4?

Hi All,

i’m trying to use the $lookup operator without success in meteor 1.4.
If i try something like:
Template.test.helpers({ rendered: function(){ var obj = mytestcollection.aggregate([ { $lookup: { from: "cat", localField: "test1", foreignField: "test2", as: "inventory_docs" } } ]) return obj } });
I get:
TypeError: collection.aggregate is not a function
Probably i don’t know how to use it or i’m doing something wrong because if mongodb 3.2 is now fully supported i shouldn’t have these problems.
I’ve waited so long for this version of Meteor just to make joins without the help of external (old) packages…

Thanks! for your help

Meteor collections don’t support aggregate. You could try to use the raw collection object.

I think your issue is that aggregate is only supported on the server, while you’re trying to do it on the client.

@dovrosenberg i tried on the server and on the client. I got the same result

@Chris.R ah, i didn’t know that. I’m trying the raw collection object right now.
I’ll let you know!

Thank you both

Strange. I can confirm that it does work - I’m using aggregate (and $lookup) in a live project using the syntax you have above (other than calling it server-side).

Yes, I tried everything and it doesn’t work.
BTW it’s quite strange that we are the only ones interested on this operator…
It could be really useful to make complex applications.

Did you try the raw collection object?
I’m not able to get the expected result with it.

You may find this post helpful. It discusses deleteOne, but the method of wrapping the aggregate method from rawCollection is the same:

1 Like

Thank you so much Rob, it worked like a charm!
Finally i can use the $lookup operator :slight_smile:

1 Like

You’re very welcome. Glad I could help.

2 Likes

It seems strange that you needed to use rawCollection, while it’s working for me with the normal Mongo.Collection object (on the server).

In any case, glad it’s working - it’s dramatically faster for joins than the code I was using before - presumably because I can now avoid all of the traffic between the database and the server.

Are you using something like meteorhacks:aggregate? That package implements the extending of the Meteor standard collection object with the underlying rawCollection().aggregate() method, which I referred to in my linked post.

I’m an idiot. You’re right. It’s been so long since i started using it in this project, I’d totally forgotten. The beauty of packages, I suppose. :slight_smile:

1 Like

It’s true that using meteorhacks:aggregate, you can make $lookup work. The result is an array though, so I can’t use it for publishing cursors.