0

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';
James Z
  • 12,209
  • 10
  • 24
  • 44
Gana
  • 1
  • 1
  • What does this function do `getPlanningOverviewModel`? – Charchit Kapoor Oct 31 '22 at 05:23
  • ```export interface PlanningOverviewDoc extends PlanningOverview, mongoose.Document {} export type PlanningOverviewDocDef = mongoose.DocumentDefinition; export const getPlanningOverviewModel = () => getMongooseConnection(module).model('PlanningOverview', planningOverview, 'planningOverview');``` This is not the problem because I am also doing all other operations like read, upsert everything using getPlanningOverviewModel(). And those are working fine – Gana Oct 31 '22 at 08:11

0 Answers0