I need to move an array element from one array to another array within the same document and delete the element from the original array it was moved from.
// Document
{
_id: ObjectId("67dc90594947be000838f7a7"),
persons: [
{
personId: "61cd90594947be000838f7c1",
name: "John Doe",
employment: [
{
employmentId: "61cd9059494abe000838f7c8",
type: "Full time",
salary: 1010101
}
]
},
{
personId: "61cd90594947be000838f7c2",
name: "Jane Austin",
employment: [
{
employmentId: "61cd9059494abe000738f7c1",
type: "Part time",
salary: 11011111
}
]
},
]
}
I need to move one of the employment element from John Doe to Jane Austin and both of those persons are in the same document. And I need to delete that moved element from John Doe.
employmentId is used to identify which employment to move. And personId is used to identify from which person to move to which person.
Example payload
const updatePayload = {
fromPerson: "61cd90594947be000838f7c1",
toPerson: "61cd90594947be000838f7c2",
employmentId: "61cd9059494abe000838f7c8",
}
Expected Result
// employment from John Doe is moved to Jane Austin
{
_id: ObjectId("67dc90594947be000838f7a7"),
persons: [
{
personId: "61cd90594947be000838f7c1",
name: "John Doe",
employment: [ ]
},
{
personId: "61cd90594947be000838f7c2",
name: "Jane Austin",
employment: [
{
employmentId: "61cd9059494abe000738f7c1",
type: "Part time",
salary: 11011111
},
{
employmentId: "61cd9059494abe000838f7c8",
type: "Full time",
salary: 1010101
}
]
},
]
}