0

I'm trying to remove the particular field from mongo document using mongoose.But it's not working.

Here my mongo document:

{
    "_id" : ObjectId("5da80eb605320f441c41e340"),
    "name" : "TEST1-_-m3mkf9",
    "users" : [ 
        {
            "formStatus" : "assigned",
            "form" : [ 
                {
                    "_id" : ObjectId("5da80eb505320f441c41e33d"),
                }
             ],
        }
    ]
}

Here my code:

await Promise.all(
  Form.update(
    { _id: "5d8da115e222df7ebc066175" },
    { $pull: { users: 1 } },
    { safe: true }
  )
)
  .then(async val => {})
  .catch(err => {
    console.log(err);
  });

I want to remove the users field from mongo document. How can i do it? Thanks in advance.

SuleymanSah
  • 17,153
  • 5
  • 33
  • 54
dhamo dharan
  • 712
  • 1
  • 10
  • 25
  • Here is [the same question](https://stackoverflow.com/questions/6851933/how-to-remove-a-field-completely-from-a-mongodb-document). Check answers – IceBro Dec 04 '19 at 12:23

1 Answers1

0

The below should work.

await Form.findByIdAndUpdate('5d8da115e222df7ebc066175', {
    $unset: { users: '' }
  });