Ir al contenido principal

Entradas

Mostrando entradas de marzo, 2021

How to delete one or more documents from your MongoDB repository

MongoDB seems to be complex at first sign, but in fact is not, you could perform a complete CRUD easily if you expend some time on the documentation. For example, lets say you need to delete some documents (rows, if you are thinking on a traditional RDBMS). First you want to find all these documents to be removed. > db.your_collection.find({ "cvectry": "MX", "layer_id": 20 }, {}) { "_id" : ObjectId("5e8123e8892355c921e6b436"), "layer_id" : 20 } > After finding all these documents to be removed, then you want to perform the following delete method as follow: > db.your_collection.remove({ "cvectry": "MX", "layer_id": 20 }, { justOne: true }) WriteResult({ "nRemoved" : 1 }) > In this case, after finding all these documents that match with "cvectry": "MX", "layer_id": 20. I got the result on the first command I ran, so I just wanted to delete just on