Removing nexted Objecte is not working because of ObjectId is not rendering correctly

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

Hello,

I tried to do sample update using ObjectID as filter, I used the sample provided int he documentation here and I just passed the ObjectID in the filter as below:

const filter = { "_id": ObjectID("636123c47df559ae2d137ff7") };

Please note that you will need to import ObjectID for this to work, otherwise you will receive the error below:

ReferenceError: ObjectID is not defined

You can find the full snippet that worked in my environment here, you can adapt it as needed for your use case:

import { MongoClient } from "mongodb";
import { ObjectID } from "mongodb";

// Replace the uri string with your MongoDB deployment's connection string.
const uri = "mongodb://127.0.0.1:27017/";

const client = new MongoClient(uri);

async function run() {
  try {
    const database = client.db("sample_mflix");
    const movies = database.collection("movies");
    // create a filter for a movie to update
    const filter = { "_id": ObjectID("636123c47df559ae2d137ff7") };

    // this option instructs the method to create a document if no documents match the filter
    const options = { upsert: true };

    // create a document that sets the plot of the movie
    const updateDoc = {
      $set: {
        plot: `A harvest of random numbers, such as: ${Math.random()}`
      },
    };

    const result = await movies.updateOne(filter, updateDoc, options);
    console.log(
      `${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`,
    );
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

I hope you find this helpful.

Regards,
Mohamed Elshafey