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

Collection.remove() should throw an error when used with an empty array, instead of removing all items #4804

Closed
AChusuei opened this issue Jul 24, 2015 · 8 comments

Comments

@AChusuei
Copy link

So, with javascript it is really easy to inadvertently pass an array object Collection.remove(array) as the selector, and if that array is the empty array, it deletes all documents in that collection!

Collection.remove() should throw an error if passed an array (like it does in mongo!) or at the very least not delete the whole collection if an empty array gets passed as the selector. This seems like a very dangerous query loophole that could easily burn beginners of Meteor.

@osv
Copy link

osv commented Jul 24, 2015

But in doc we can find:

Set selector to {} if you really want to remove all documents from your collection.

Also, I think beginners must know, that better use some "remove" flag instead of deleting record.

@lukejagodzinski
Copy link
Contributor

@osv empty array is not the same as empty object.
@AChusuei in deed, passing an array removes all the records when in Mongo it returns error.
In my opinion it should be fixed to match the way the Mongo database does it.

@AChusuei
Copy link
Author

@jagi 👍 absolutely. Let's say this burned me just now ...

@AChusuei
Copy link
Author

@osv, for a less seasoned js and mongo programmer, it feels idiomatic to pass an array of ids to a query, and it's really easy to do this if you are not that versed in the mongo selector query style.

@stubailo stubailo changed the title Collection.remove() removes all documents when an empty array is passed as the selector Collection.remove() should throw an error when used with an empty array, instead of removing all items Jul 28, 2015
@stubailo
Copy link
Contributor

I'm not sure if it's idiomatic or not, but I agree that Mongo should probably throw an error instead of silently accepting the wrong type of argument, worse still if that results in deleting all items from the database.

@AChusuei
Copy link
Author

Woo! Thanks @stubailo! =)

@AChusuei
Copy link
Author

@stubailo, would it be possible at all to include me in the PR when you get around to doing this? Just curious to see how this would be implemented.

stubailo pushed a commit that referenced this issue Jul 29, 2015
@AChusuei
Copy link
Author

@stubailo, looks great thanks! ^_^

stubailo pushed a commit that referenced this issue Jul 30, 2015
stubailo pushed a commit that referenced this issue Jul 31, 2015
Fix #4804 and throw error when Mongo selector is array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants