배열인 필드에 값 추가하기
// 신규 그룹 생성시 유저에 그룹 objectId 추가
async addGroupIdFromGroup(id: string, group: any) {
// 해당 유저로 생성한 그룹아이디를 추가한다.
const updateUser: any = await this.userModel.updateOne(
{ _id: id },
{
$push: { groups: { _id: group._id } },
},
);
if (!updateUser) throw new HttpError(404, "Not Found");
return updateUser;
}
- '$push'를 쓰면 조회한 결과의 필드(배열)에 값을 추가함
배열인 필드에 값 제거하기
// 그룹 삭제시 해당유저의 그룹아이디도 삭제
async removeGroupFromUser(_id: any, groupId: string) {
await this.userModel.updateOne(
{ _id },
{
$pullAll: { groups: [groupId] },
},
{ new: true },
);
}
- 'pull' or 'pullAll'로 해당하는 값을 제거할 수 있음