How to remove the nested array object value with its Object ID in Mongoose?

Elamparithi.P

i need to delete the url from the below mongodb database with the id which is in url object, then need the remaining data's as a result

My mongodb collection has the following :

    {
      "_id" : ObjectId("589b043abc2f5a467c13303b"),
      "user_id" : ObjectId("5874c174c813822341cb59a7"),
      "filename" : [
       {
         "url" : "images/product_images/file-1486554170465.jpeg",
         "_id" : ObjectId("589b043abc2f5a467c13303c")
       },
       {
         "url" : "images/product_images/file-1486554306440.jpeg",
         "_id" : ObjectId("589b04c2bc2f5a467c13303f")
       }]
    }

Can anybody help me please , thanks in advance ..

Jyotman Singh

In mongoose you can do this -

CollectionInstance.findByIdAndUpdate(documentId, {
    $pull: {
        filename: {_id: urlId}
    }
}, {new: true});

By documentId I mean 589b043abc2f5a467c13303b and by urlId I mean 589b043abc2f5a467c13303c in your above example code.

What we are doing here finding is the particular document in the collection through its _id and then pulling the particular document from the filename array through its _id.

The {new: true} option tells Mongo to return the updated document. By default this value is false and Mongo returns the document state before update.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove object from array knowing its id

How to remove Object from array using mongoose

Mongoose only saves array of object _id value

mongoose remove an object from a nested array

How to add object to nested array in mongoose?

combine an array of object by id and and reduce its nested object

Remove object by id from an array in mongoose

Remove nested array object based on id and type?

Mongoose nested schema array - Object has no method 'id'

Remove or Find an object with Mongoose when it is in an array by the object property value

Find and remove object in array by value in nested array

Remove an object by an id in mongodb and mongoose

How to get value form nested object in mongoose?

How to remove an object of an array if its ID is the same of an other arrays object

How to return object in an array with certain value with mongoose?

How to find object with id value in deep nested array?

Remove object from array if nested object has certain value

How to access an object nested inside an array by the value of one of its keys?

Mongoose update value in object nested in array in nested document

query nested array of object mongoose

Flatten and sort nested array of object by its array value

Update nested object in an array in mongoose using its id

How to remove object key value of an object array?

Mongoose - How to push object in nested array of objects

Mongoose - Push value to array which is nested in object which is nested in array

Remove value from an array nested in an object

How to update nested object in array (Mongoose/MongoDB)

How to update a nested object in array that is in the array of object in mongoose

Mongoose Update Nested Array Object