0

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 ..

3
  • You can use the select(['field1', 'field2']) from Mongoose. Or you can do some post-processing with _.pick() or _.omit() from the Lodash library. Commented Feb 10, 2017 at 5:59
  • What all information do you have to fetch the document? Do you have the _id of the document and also the _id of the filename you want to remove? Commented Feb 10, 2017 at 6:34
  • i have userid,id of url and whole collection id , i need to remove the url . Commented Feb 10, 2017 at 6:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

'new_add_schema.findByIdAndUpdate("589d64e9e4aee71251c56941", { $pull: { filename: { "_id": "589d6630866a0c1429f7e96a" } } }, { new: true }, function(req,vft) { console.log(vft) });'
I try the above , but its not delete the object(url and id)
woooow , its working fine now , after 3 days i get this result . thanks !! thanks a lot Jyotman Singh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.