I have a posts model in mongoose as follows:
let PostSchema = new mongoose.Schema({
message: {
type: String,
default: 'NA'
},
likeCount: {
type: Number,
default: 0
},
likes: [{
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
}]
});
all the likes users will be stored in likes array. Now I'm using following request
/get_posts/:userID
where userId is ID of user, now I want to write a query to get result like following:
{
"posts": [
{
"_id": "5ae1975acf8db214e4a920cf",
"message": "test message",
"likeCount": 1,
"isLiked": true
},
{
"_id": "5ae2cf410cc9401ce4cd0bf6",
"message": "test message 1",
"likeCount": 2,
"isLiked": false
}
}
Here "isLiked" is true if the user ID present in likes array. Please help me to write query