1

i am a beginner in mongoose and i am trying to delete an element in array but i am facing troubles to do it

my schema is like this

    name : {
        type: String,
        trim : true
    },
    pictures: {
        type : Array,
        required : false
    },

and in my database it looks like this

{
    "_id" : ObjectId("5eb67e7c74b35b205362b7f4"),
    "title" : "john doe",
    "pictures" : [ 
        "img1.jpg", 
        "img2.png", 
        "img3.jpg", 
        "img4.jpg", 
        "img5.jpg"
    ]
}

how can i delete an item in the array of pictures?

1 Answer 1

2

You need $pull operator:

await Model.updateOne({"_id" : ObjectId("5eb67e7c74b35b205362b7f4")}, {$pull: { "pictures": "img1.jpg"}});
Sign up to request clarification or add additional context in comments.

2 Comments

it does not work but as you have showed me the way, i am now trying to make it work
i just replaced the objectId by plain id and now it works!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.