Hi all,
I am facing some problems while removing a nexted document.
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 purpose I tried 2 senarios in 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 1)
getPlanningOverviewModel().updateOne({ _id: id}, { $pull: { iLevelPlanning: { _id: iLPId} } }).exec();
attempt 2)
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 matched object.
And I have also imported mongoose as well
import mongoose from ‘mongoose’;
If anybody knows the answer, kindly reply