i am new in the field mern stack and i want your help to remove to remove comment from User data
which is equal to Post data
comment
Post Data: {
_id: new ObjectId('66985b5774dd3a87fd1be9d4'),
title: 'adadasd',
description: 'adasda',
category: 'Web Development',
user: new ObjectId('6692abbc21ab34ea540b3755'),
role: 'Blogger',
bio: 'Lorem ipsum dolor, sit amet consectetur adipisicing elit.',
comments: [
{
_id: new ObjectId('66985b6074dd3a87fd1be9dc'),
user: new ObjectId('6692abbc21ab34ea540b3755'),
message: 'adad',
post: new ObjectId('66985b5774dd3a87fd1be9d4'),
createdAt: 2024-07-18T00:01:36.508Z,
updatedAt: 2024-07-18T00:01:36.508Z,
__v: 0
},
{
_id: new ObjectId('66985b6374dd3a87fd1be9e4'),
user: new ObjectId('6692abbc21ab34ea540b3755'),
message: 'dada',
post: new ObjectId('66985b5774dd3a87fd1be9d4'),
createdAt: 2024-07-18T00:01:39.457Z,
updatedAt: 2024-07-18T00:01:39.457Z,
__v: 0
}
],
User data: {
_id: ObjectId('66985b5774dd3a87fd1be9d4'),
title: 'adadasd',
description: 'adasda',
category: 'Web Development',
image: 'https://res.cloudinary.com/dgmfnfvf9/image/upload/v1721260913/blog-app-v3/ty1qyrqyodklxqwmf7us.jpg',
user: ObjectId('6692abbc21ab34ea540b3755'),
role: 'Blogger',
bio: 'Lorem ipsum dolor, sit amet consectetur adipisicing elit.',
comments: [
ObjectId('66985b6074dd3a87fd1be9dc'),
ObjectId('66985b6374dd3a87fd1be9e4'),
ObjectId('66985f4746d06f903adef17f'),
ObjectId('66986298273a46211122814c')
],
createdAt: ISODate('2024-07-18T00:01:27.733Z'),
updatedAt: ISODate('2024-07-18T00:32:24.142Z'),
__v: 0
},
and the code i am trying to run is
app.post('/remove',async (req,res,next)=>{
try {
const post = await Post.findById(req.params.id,{
comments: 1,
_id: 0
});
//if post is deleted then remove post id from user post
await User.updateOne({post: req.params.id},{
$pullAll: {
comment: {$in: post.comments}
},
});
} catch (error) {
return res.render("posts/postDetails",{
error: error.message,
post: ""
});
}
});
I just want to remove Comment of User data
whose id is equal to Post Data
I think that a few errors/things that need to be clarified in your question.
-
There is no
post
field in the User document. So your update query will never work. Your Post and User data are odd as they refer to the same ID. This makes the relationship become 1-to-1, by right a user can post multiple posts and a post is only created by one user (1-to-many, ignore nowadays Facebook, Instagram platforms support feature with one post can have multiple (associate) post owner).I leave here to let you re-think your implementation of the relationship between the User and Post collections.
The actual correct way should be when the post is deleted:
1.1. Get its
user
values in thecomments
array.1.2. Fetch the User documents by the value from (1.1).
1.3. Update the User documents for the
comments
array field by pulling the element(s) based on thecomments
‘_id
field from the deleted post. -
The
post.comments
returns an array of objects, while you can’t use it in the$pullAll
operator for thecomments
(typo error in your query) array in the User document, as thecomments
is an array ofObjectId
s. They are different types.
From $pullAll
documentation:
$pullAll
removes elements that match the listed values.
For the fixes:
-
Change the
post
field to_id
for referring based on your current implementation. -
Provide the comment’s
_id
array to thecomments
array for the$pullAll
operator
await User.updateOne({ _id: req.params.id}, {
$pullAll: {
comments: post.comments.map(x => x._id)
}
});
Demo @ Mongo Playground