Search code examples
node.jsmongodbmongoosemongoose-schemamongoose-populate

Mongoose filter elements based on other collection


I have three collections :

  1. User which has some user data

    id: ObjectId

  2. Post which is a post an User can make - it has a ref to User.id

    id: ObjectId author: {ref: 'User'} text: String datePosted: Date

  3. User.reports which is an association between User.id and Post.id, a document in this collection means that an User has reported a Post.

    id: ObjectId user: {ref: 'User'} post: {ref: 'Post'} dateReported: Date

The Posts can be huge (thousands), so when I retrieve them I do some infinite scroll - get the 10 latest, then the next 10 etc....

This is my query :

        let posts = await Post.find()
        .skip(start) //0 initial
        .limit(step) //step is 10
        .populate('author')
        .sort('-datePosted')
        .exec();

    return res.json(posts);

My question is : How do I skip the Posts reported by the User - which are in the User.reports collection, and always return the right amount of Posts (in this case 10) ?


Solution

  • I see what you’re getting at now. So, basically you want to use the $nin operator in your find query to eliminate ids that are in an array of Post ids that the user has reported.

    In your current schema, that means running two queries, first to retrieve reported post ids from your reports collection matching the user, and then running the query like:

       Post.find( { _id: { $nin: arrayOfPostIds }}).....
    

    If you are able to just put the reports in the user schema it’s easier, as it saves a query, but I recognize there are cases where it’s better to have them in their own collection too.