0

I want to delete several elements at once from MongoDB. For this purpose in the backend I want to use deleteMany. But first I need to assign array to ids, then build a query and then perform a delete

app.delete('/tasks', function(req,res){
  ids = [];
  var myquery = { _id: { $in: ids } };
  Model.collection.deleteMany(myquery, function(err, result) {
    if (err) throw err;
    if (result) {
      res.json(result)
    }
  });
});

My problem is that I don't know how to pass the ids array of IDs to backend from frontend. I tried with requestParameters, but it failed...Also I tried RequestOptions, but it is now depreciated...I guess, I should add all IDs as params? How could I iterate trough ids and add it to params? Or maybe there is other better way to this?

 deleteData(ids) {
//ids is array of IDs, which I would like to pass as requestOptions

    return this.http.delete( this.api, requestOptions);
  }
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Anna F
  • 1,583
  • 4
  • 22
  • 42
  • You need to set the `ids` ( presumably already a list ) as a value in the request body. Then your express back end needs to read this data from `req.body`. Presuming you have already set up the bodyParser middleware this should be straightforward. If you have not yet set that up then please say so in your question. May I also ask why you are calling with `Model.collection.deleteMany()` instead of `Model.deleteMany()`. Do you have a real reason, or is this just some code you copied from somewhere else? – Neil Lunn Jun 02 '18 at 07:31
  • See https://stackoverflow.com/questions/45525753/restful-api-and-bulk-operations – Jota.Toledo Jun 02 '18 at 08:15
  • Is there something in the provided answer that you believe does not address your question? If so then please comment on the answer to clarify what exactly needs to be addressed that has not. If it does in fact answer the question you asked then please note to [Accept your Answers](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) to the questions you ask – Neil Lunn Jun 05 '18 at 20:45

1 Answers1

1

On reflection, a DELETE verb probably is not appropriate for a "list" of content and this would be better suited to a POST or PUT or even PATCH. For all of the other verbs it's generally accepted that these will have a request body which is suitable for keeping data such as a "list".

Generally there is nothing wrong with POST and whilst it's not "official" that DELETE should not send a content body, you are probably better off sticking with standards, at least from the perspective of what would be most widely supported should your "front end" end up talking to a different "back end" service structure at some later stage.

Therefore if we POST instead, then your angular method would be:

deleteData(ids) {
  // presuming this.api points to /tasks at the correct point
  return this.http.post( this.api, { ids });
}

On the express side you would want to have the body-parser middleware set up if you have not already:

const bodyParser = require('body-parser');
app.use(bodyParser.json());

And then in your actual route controller, simply return the array from req.body with a specifically named route for the POST request:

app.post('/tasks/delete', function(req,res) {
  var myquery = { _id: { $in: req.body.ids } };
  Model.deleteMany(myquery, function(err, result) {
    if (err) throw err;
    if (result) {
      res.json(result)
    }
  });
});

Noting also that mongoose supports deleteMany() directly on the model, so there is no need to use the collection accessor to the underlying driver Collection object. Also using the mongoose method ensures that if your list of ids are actually meant to be ObjectId values ( as they typically are for _id ) then they will "autocast" from the strings as which they were sent from the client. Actually whatever the schema type is for the field in any query, mongoose will take care of this when using it's own methods.

Using DELETE really should be reserved for single parameter requests "within the url" such as:

app.delete('/tasks/:id', function(req,res) {
  var myquery = { _id: req.params.id };
  Model.deleteOne(myquery, function(err, result) {
    if (err) throw err;
    if (result) {
      res.json(result)
    }
  });
});
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317