dsanel / mongoose-delete

Mongoose Soft Delete Plugin
MIT License
346 stars 101 forks source link

Error when overriding "aggregate" method and using aggregation pipelines with "$geoNear" #124

Closed fegodev closed 1 year ago

fegodev commented 2 years ago

What's happening?

When you set the overrideMethods option to all or add aggregate to its list you will see this error when running Mongo's .aggregate method with "$geoNear" in its first stage:

"$geoNear is only valid as the first stage in a pipeline."

Why is this happening?

During override the deleted filter is added to the top of the pipeline. It should have a fallback if $geoNear is in the first stage somewhere around here: https://github.com/dsanel/mongoose-delete/blob/313f434ae825c2de4869284ae24eb95cdef82ee1/index.js#L142

Workaround?

Remove aggregate from overrideMethods option and exclude deleted documents manually in your aggregation.

guillaume-gagnaire commented 1 year ago

Or simply use aggregateWithDeleted, and add {deleted: false} to your $geoNear stage.