1
{
  "_id": "608c3d353f94ae40aff1dec4",
  "userId": "608425c08a3f8db8845bee84",
  "experiences": [
    {
      "designation": "Manager",
      "_id": "609197056bd0ea09eee9429c"
    },
    {
      "designation": "Asst. Manager",
      "_id": "608c530de8ade5221b0e6d4e"
    },
    {
      "designation": "Sr. Manager",
      "_id": "608c534be8ade5221b0e6d4f"
    },
  ]
}

I want to delete object in array with id 608c530de8ade5221b0e6d4e, here is my code but that gives me error.

This is the controller:

const userId = req.userData.userId;
const id = req.params.id;
    
Experience.findOneAndUpdate({ userId: userId }, { $pull: { 'experiences': { '_id': id } }}, { multi:true }, function(err, obj) {
                  //   //do something here
});

This is model:

const newExpSchema = new mongoose.Schema({
  designation: { type: String,  default: ""},
});

const experienceSchema = new mongoose.Schema({
  userId: { type: String, required: true },
  experiences: [newExpSchema],
});

export default model("experience", experienceSchema);

I am getting below error on { $pull: { 'experiences': { '_id': id } }} Error:

No overload matches this call.
Overload 1 of 3 .........
.......
The expected type comes from property '$pull' which is declared here on type 'UpdateQuery<Document<any, {}>>'

1 Answer 1

2

Can you try this:

Folder.findOneAndUpdate({
  "_id": "608c3d353f94ae40aff1dec4"
},
{
  $pull: {
    "experiences": {
      "_id": "608c530de8ade5221b0e6d4e"
    }
  }
},
{
  "multi": false
})

Here is a working example: https://mongoplayground.net/p/YtNGBTr52U9

Sign up to request clarification or add additional context in comments.

8 Comments

oh sorry, these numerics are just for example, actual strings are there.
Can you test it in working example? Does it work?
No overload matches this call. I am getting this same error after using your solution.
But it is working in the demo that I added to question, and I used the same data from your question. Can you copy your exact code to the question?
I have updated the question with real data and code. Please take a look
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.