I am developing a nodejs backend for an application in which a user will be able to see the posts created by the users he/she follows.
I want to do this using mongoose.
My userSchema:
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
hashed_password: {
type: String,
required: true
},
isVerified: {
type:Boolean,
default: false
},
emailVerificationToken: String,
emailVerificationTokenExpires: Date,
followers: [{ type: ObjectId, ref: 'User'}],
following: [{ type: ObjectId, ref: 'User'}],
posts: [{type: ObjectId, ref: 'Post'}],
bio: String,
website: String
});
My postSchema:
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
url: {
type: String,
required: true
},
description: {
type: String,
},
website: String,
tags : [String],
createdBy: { type: ObjectId, ref: 'User'}
});
I have tried this:
User.findById(currentUserId)
.populate("following","posts")
.populate("following.posts","_id title")
.skip()
.limit()
.exec(function(err,users){
res.send(users);
});
This is returning paginated users with followers and their posts populated. I only want posts of all users. Can anyone please help me through this?