Home > OS >  How to delete object inside an array in mongodb collection?
How to delete object inside an array in mongodb collection?

Time:04-12

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related