• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

98
Views
How to delete object inside an array in mongodb collection?

i have a controller function as:

User.updateOne(
    { _id: "6252d87e010b3f94adcf4e41" },
            {
                $pull: {
                    newCart: {
                        "post._id": "6249e216c1e3b6ae2d4aa273",
                    },
                },
            },
            {new:true}
        ).then((res) => console.log(res, user));

The response is like this:

{
  acknowledged: true,
  modifiedCount: 1,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}

It shows modifiedCount to 1 but never changes in my realtime database collection.

My collection looks like this: Image of my db collection

2 months ago ·

Santiago Trujillo

2 answers
Answer question

0

The query is correct maybe try adding exec

User.updateOne(
  { _id: "6252d87e010b3f94adcf4e41" },
  {
    $pull: {
      newCart: {
        "post._id": "6249e216c1e3b6ae2d4aa273",
      },
    },
  },
  { new: true }
)
  .exec()
  .then((res) => console.log(res, user));

Hope this will solve the issue.

2 months ago · Santiago Trujillo Report

0

your query is completely fine. disconnect your database and try to connect again. after restart maybe this will work

2 months ago · Santiago Trujillo Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.