DEV Community

Nikhil
Nikhil

Posted on

To delete element of an array in mongoose collection

I'm having a Mongoose collection(Sparks) in this format:
{
_id: ObjectId("id1");
title: string;
description: string;
type: "USER" | "ADMIN";
created_at: Date;
status: "ACT" | "DEL";
categories: {
is_trending: boolean;
is_top: boolean;
};
related_sparks: ["id2", "id3",..];
},
{
_id: ObjectId("string");
title: string;
description: string;
type: "USER" | "ADMIN";
created_at: Date;
status: "ACT" | "DEL";
categories: {
is_trending: boolean;
is_top: boolean;
};
related_sparks: ["id1", "id2", "id3",..];
},
{
_id: ObjectId("string");
title: string;
description: string;
type: "USER" | "ADMIN";
created_at: Date;
status: "ACT" | "DEL";
categories: {
is_trending: boolean;
is_top: boolean;
};
related_sparks: ["id1", "id2", "id3",..];
}

When I delete first spark with ObjectId("id1"), then the same "id1" should be removed from the related_sparks array of every other collection, if present. How is it possible?

Top comments (0)