카테고리 없음

[Node.js] Mongooes 유용한 함수 모음

완드로이드 2024. 8. 12. 19:54
const commentsToDelete = await Comment.find({
            $or: [
                { _id: commentId },
                { responseTo: commentId }
            ]
        }).select('_id');

$or 옵션 :둘 중 하나라도 조건이 맞는 필드를 가져오는데 'id' 값만( .select('_id'); )만 가져옴

 

const commentIdsToDelete = commentsToDelete.map(comment => comment._id);

        // 찾은 모든 댓글을 삭제합니다
        await Comment.deleteMany({ _id: { $in: commentIdsToDelete } });

$in옵션: 해당 하는 모든 데이터를 선택 ex)여기선 deleteMany를 사용하여 해당하는 모든 데이터 삭제