Suppose I have an object in mongoDB:
collection_name: book_info
{_id:"121as", "book_num":"12a", "book_name":"lotr", "info":[ {"borrowerAddress":["NY","Delhi"] },
{"borrowerAddress":["SI","Ghana"] } ] }
I want to delete an element "NY" from info.
For this I can do is fetch the object from book_info collection and apply loop and delete when element found inside array and save the data into db.
But I want to do is delete and update at same time without fetching data and looping and updating again.
Also, if direct db manipulation is possible, can anyone suggest me whether fetching data, applying loop and deleting data is efficient or deleting data from db directly is more faster, based on solutions provided.
If anyone needs any further information, please let me know.
Additional Question:
doc1: {_id:"121as", "book_num":"12a", "book_name":"lotr", "info":[ {"borrowerAddress":["NY","Delhi"] },
{"borrowerAddress":["SI","Ghana"] } ] }
doc2: {_id:"213s", "book_num":"1c", "book_name":"hp", "info":[ {"borrowerAddress":["NY","Delhi"] },
{"borrowerAddress":["SI","Ghana"] } ] }
Assume I have multiple documents inside a book_info collection with different book names and details and I want to delete document for "book_name":"lotr" only, I delete address "NY" from document.