How to remove data from mongo dab using $pull operator

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.

  1. 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 the comments 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 the comments_id field from the deleted post.

  2. The post.comments returns an array of objects, while you can’t use it in the $pullAll operator for the comments (typo error in your query) array in the User document, as the comments is an array of ObjectIds. They are different types.

From $pullAll documentation:

$pullAll removes elements that match the listed values.

For the fixes:

  1. Change the post field to _id for referring based on your current implementation.

  2. Provide the comment’s _id array to the comments array for the $pullAll operator

await User.updateOne({ _id: req.params.id}, {
  $pullAll: {
    comments: post.comments.map(x => x._id)
  }
});

Demo @ Mongo Playground

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật