Mongo - How to do that query?

Hi !

I have two mongo collections :

artworks => {

artworks_favorites => {

So when user “likes” an artwork, I add a value in the artwork_favorites collection.
This collection is easy to querying when I want to know the count of like for an artwork for exemple. Or to know if user already liked an artwork.

But know I want get the top 10 of the most liked artworks.

How to make this request ? I think I need to use publish-composite to get reactive data. But I don’t know the best way to achieve this query.


I would probably maintain a likesCount property on your artworks collection, rather than dive into something like publish-composite. That way you can still keep your queries simple and performant.

1 Like

Either what @vigorwebsolutions said, or maintain an array of user _ids who have liked it, that way people can’t double like - and you can still track which users liked what.

You could try my Likeable package which is specifically for implementing this functionality. It keeps a count on the parent record automatically, a record of each like and who took the action, and only allows a user to like each parent record once.

@copleykj this must only work on the server right?

No, the package enables complete client side operations as do all of the Socialize packages.

That is interesting! (sorry for the side track OP)
So does that mean a client can pull any other users Meteor.user() object?

No, not at all. Security is tightly controlled through the use of Simple Schema, allow/deny, and collection-hooks. Publications are left in your hands so you can publish as little or as much information as your application requires. Many times you’ll just want to publish the parent and the corresponding like record for the current user if there is one so you can show the user if they liked it or not.

1 Like

Ahh nice, it is a really well thought out package.
Thanks for the info :slight_smile:

Thanks all for your reply :slight_smile:

@cstrat if an artwork have 1 billion likes, they are 1 billion user id in the array.
It’wll be a problem with the size of the collection ?

@copleykj with your package, how can I make this query ?
I see that you have a likeCount() function, but how to request mongo to have the top liked artworks ?

You would need a billion user accounts for that… I don’t think it’s an issue.

You would simply sort on the _likeCount field of the artworks collection and limit the results to 10.

const top10artworks = ArtworksCollection.find({}, {
    sort:{ _likeCount: -1 }, 
    limit: 10

Hum ok, it’s pretty easy then.

LikeableModel.LikeableSchema = new SimpleSchema({
    _likeCount: {
        custom: SimpleSchema.denyUntrusted

I’m not sure what you mean by that. Do you mean simple to use the package or simple to implement yourself?

I mean with your package the query is very easy :slight_smile:
Thanks for that :slight_smile:

haha, ok. I think the code just threw me off :smile: