feathersjs-ecosystem / feathers-mongoose

Easily create a Mongoose Service for Feathersjs.
MIT License
189 stars 96 forks source link

How to make .remove(null) not to return all removed documents #439

Open fmoessle opened 1 year ago

fmoessle commented 1 year ago

MongoDB: 4.2.17 feathers-mongoose: 8.5.1 @feathers/feathers: 4.5.15

Hi!

Currently calling .remove(null) on a service returns all removed documents.

// assuming we have a service called "books"
await feathers.service('books').create({ title: 'The Lord of the Rings' })

// this will return [{ title: 'The Lord of the Rings' }]
await feathers.service('books').remove(null)

// this will also return [{ title: 'The Lord of the Rings' }]
await feathers.service('books').remove(null, {
  query: {
    $limit: 0
  }
)

While this is fine for a small number of removed documents, when the number get's higher (in our case 50k+) things start to get very slow.

Is there a way to avoid this?

DaddyWarbucks commented 1 year ago

This should also be implemented for create and patch. See also: https://github.com/feathersjs-ecosystem/feathers-mongodb/issues/219

DaddyWarbucks commented 1 year ago

@fmoessle The best way to avoid this until the library is updated is to use an after hook to unset the results. For example

// I wouldn't use $limit: 0 just yet. That may cause patch/remove to not
// actually update any records? I know your example says it works and that is
// good, but I am not sure thats the case for patch too? So I wrote this to use
// $returning similar to feathers-sequelize until we can ensure $limit: 0 is
// correct.
const handleMulti = (context) => {
  const { id, params } = context;

  if (id) {
    return context;
  }
  // if (params && params.query && params.query.$limit === 0) {
  //   context.result = [];
  // }

  if (params && params.query && params.query.$returning === false) {
    context.result = [];
  }

  return context;
};