After the deletion of a chore, (which is made elsewhere in a separate call) I would like to remove all the entries in the 'list' that contain a reference to the chore that was deleted.
I have the following staticAssignment schema which contains a reference to the chore.
const CP_Schema = new mongoose.Schema({
REF_chore: { type: Schema.Types.ObjectId, required: true, ref: 'cm_chores' },
REF_person: { type: Schema.Types.ObjectId, required: true, ref: 'cm_users' },
type: { type: String, required: true, },
when: [{ type: Number, required: true, }],
});
const StaticAssignmentSchema = new Schema({
affiliation: { type: String, required: true },
list: [{ type: CP_Schema }],
});
module.exports = StaticAssignment = mongoose.model('cm_staticassignments', StaticAssignmentSchema);
Here is some sample data that can be added to a test NOTE: there are two entries which contain the chore ID that I want to remove '5c6f12cf4b19711d1824b16f'. The third entry should remain untouched.
{
"affiliation": "800_800",
"list": [
{
"when": [
-1
],
"type": "daily",
"REF_person": "5e441bd5332359211c0403fb",
"REF_chore": "5c6f12cf4b19711d1824b16f"
},
{
"when": [
3
],
"type": "weekly",
"REF_person": "5e441be9332359211c0403fc",
"REF_chore": "61a261a4ddd46f623879e8df"
},
{
"when": [
7,
14,
21,
28
],
"type": "monthly",
"REF_person": "61880980339cab46cccadd4d",
"REF_chore": "5c6f12cf4b19711d1824b16f"
}
]
}
I am trying to remove all entries from the "list" using mongo's updateOne with a $pull for the chore ID. However the mongo DB call is not working as I expect it to work.
var query = { affiliation: '800_800' };
var pull = { $pull: { list: { $elemMatch: { REF_chore: mongoose.Types.ObjectId('5c6f12cf4b19711d1824b16f') } } } };
var ret = await StaticAssignment.updateOne(query, pull);
Here is what I am getting back after the 'updateOne' call is executed:
{
"n": 1,
"nModified": 0,
"opTime": {
"ts": "7037976996000825345",
"t": 88
},
"electionId": "7fffffff0000000000000058",
"ok": 1,
"$clusterTime": {
"clusterTime": "7037976996000825345",
"signature": {
"hash": "+iVghD+zcvGAOak8oAKgNknZGJQ=",
"keyId": "6977092102253445122"
}
},
"operationTime": "7037976996000825345"
}
Additional info
Here is a snap shot of my mongo collection showing that REF_chore is an object id: