Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not use $ne operator #50

Closed
gabzim opened this issue Oct 31, 2018 · 6 comments
Closed

do not use $ne operator #50

gabzim opened this issue Oct 31, 2018 · 6 comments

Comments

@gabzim
Copy link

gabzim commented Oct 31, 2018

In large collections, the $ne operator is very inefficient and can make queries slow. (https://docs.mongodb.com/manual/reference/operator/query/ne/ last paragraph).

I recently had to investigate performance troubles we're having in one of our largest collections and it's due to the use of this plugin. What I'd suggest is maybe always keeping deleted as false and then doing {$eq: false} to retrieve active documents.

@tcstory
Copy link

tcstory commented Nov 1, 2018

assume I had the following documents, the documents were producted before I use mongoose-delete plugin,

{_id: 'xx', name:1},
{_id: 'xx', name:2},
{_id: 'xx', name:3},

and now I start to use this plugin, because it use $ne operator, so it will treat these documents as undeleted, this is what I need. but if it use $eq operator, I have to add deleted: false to every documents.

@gabzim
Copy link
Author

gabzim commented Nov 1, 2018

True, but if your collection has 1,000,000 docs instead of 3, a query that does not filter them out by an index will timeout. And this can get unpredictable as it will happen in some collections and it won't in others.

@bdelville
Copy link

bdelville commented Nov 18, 2018

Until proposing a pull request, a quick fix is:
countWithDeleted(Object.assign({deleted: false}, query));

That supposed that "deleted" always exist, which is the case in my database

@bdelville
Copy link

It is in pull request

dsanel added a commit that referenced this issue Sep 3, 2019
#52 Counter mongo index bug with $ne and count()
#50 do not use $ne operator
#56 Fix Mongoose DeprecationWarning: collection.update is deprecated
@dsanel
Copy link
Owner

dsanel commented Sep 3, 2019

This type of changes can affect existing users and I really try to keep back compatibility with older versions. This is the main reason why I introduce new option use$neOperator so you can override default behavior (default is {use$neOperator: true}).

This new option will cover both scenarios.

https://github.com/dsanel/mongoose-delete/blob/master/test/index.js#L1598-L1654

Hope this will help @gabzim? ;)

@dsanel dsanel closed this as completed Sep 3, 2019
@simplenotezy
Copy link

FYI I just tried doing a comparison, and inside a collection of +1mn documents, and I didn't see any noticeable difference.

Am I missing something?

Using deleted: {$ne: true}

image

Using deleted: false

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants