I am facing problems while removing a nested document in mongo db using mongoose. The schema looks like this
const iLevelPlanning = new mongoose.Schema({
istep: String,
maturity,
milestone: [milestone],
buildingPhase: [buildingPhase]
});
const planningOverview = new mongoose.Schema({
brv: String,
introduction: String,
pl: String,
iLevelPlanning: [iLevelPlanning],
comment: [comment],
modified: Date,
});
For debugging purposes, I tried 2 scenarios in the mongo shell
Attempt 1
db.getCollection('planningOverview').updateOne({ _id: "62ecfcd1caae1e0000beb8ee" }, { $pull: { iLevelPlanning: { _id: "63172667f1242f000012b93f" } } })
Attempt 2
db.getCollection('planningOverview').updateOne({ _id: ObjectId("62ecfcd1caae1e0000beb8ee") }, { $pull: { iLevelPlanning: { _id: ObjectId("63172667f1242f000012b93f") } } })
The first one did not work. But the second one worked.
Now I have to do this from my node application.
I tried something like this here also
Attempt 3
getPlanningOverviewModel().updateOne({ _id: id}, { $pull: { iLevelPlanning: { _id: iLPId} } }).exec();
Attempt 4
getPlanningOverviewModel().updateOne({ _id: mongoose.Types.ObjectId(id)}, { $pull: { iLevelPlanning: { _id: mongoose.Types.ObjectId(iLPId) } } }).exec();
Of course, the first one did not work, however, the second code also not updating in the database. It's not finding the matched objects.
And I have also imported mongoose as well
import mongoose from 'mongoose';