This particular schema represents a single document out of thousands in the database, all housed within the same collection.
Document 1:
{
pageNumber: 0,
results: [
{
jobkey: "AAA",
},
{
jobkey: "BBB",
},
{
jobkey: "CCC",
}
]
}
Document 2:
{
pageNumber: 0,
results: [
{
jobkey: "RRR",
},
{
jobkey: "VVV",
},
{ //This Entire Object needs to be removed
jobkey: "AAA", //Duplicate jobkey value of document 1
//remaining objects in array should stay
}
]
}
In this structure, each document contains a key called "results" that holds an array of objects. These objects consist of a jobkey and its corresponding value. Importantly, no two jobkeys within the same results array can have the same value.
The Problem:
If there is a duplicate jobkey value across different documents or within the same document, I require one of the duplicates to be deleted from the database. This situation might occur where the same jobkey exists in multiple result arrays.
Despite efforts in the mongo shell and through mongoose, a solution to remove these duplicate values has proven elusive.