Node.js – mongoose distinct and populate with documents

mongodbmongoosenode.js

I have the following model:

var followerSchema = new Schema({
    id_follower: {type: Schema.Types.ObjectId, ref: 'Users'},
    id_post: {type: Schema.Types.ObjectId, ref: 'Posts'}
});

I want to be able to find all posts for a list of followers. When I use find, it returns me of course multiple times the same post as multiple users can follow the same post.

So I tried to use distinct, but I have the feeling the "populate" does not work afterwards.

Here is my code:

followerModel
    .distinct('id_post',{id_follower:{$in:followerIds}})
    .populate('id_post')
    .sort({'id_post.creationDate':1})
    .exec(function (err, postFollowers) {
        console.log(postFollowers);
    })

It only returns me the array of the posts, and it is not populated.

I am new to mongoDB, but according to the documentation of mongoose, the "distinct" method should return a query, just as the "find" method. And on a query you can execute the "populate" method, so I don't see what I am doing wrong.

I also tried to use the .distinct() method of the query, so then my code was like this:

followerModel
    .find({id_follower:{$in:followerIds}})
    .populate('id_post')
    .distinct('id_post')
    .sort({'id_post.creationDate':1})
    .exec(function (err, postFollowers) {
        console.log(postFollowers);
    })

In that case it works, but as in the documentation of mongoose you need to provide a callback function when you use the distinct method on a query, and so in my logs I get errors all over. A workaround would be to have a dummy callback function, but I want to avoid that…

Does anybody has an idea why the first attempt is not working? And if the second approach is acceptable by providing a dummy callback?

Best Answer

Would this be the right way considering the current lack of support in mongoose?

followerModel
.find({id_follower:{$in:followerIds}})
.distinct('id_post',function(error,ids) {
   Posts.find({'_id':{$in : ids}},function(err,result) {
     console.log(result);
   });
});