[SERVER-12409] db.coll.remove() should not default to db.coll.remove({}) Created: 18/Jan/14  Updated: 09/Mar/14  Resolved: 10/Feb/14

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 2.6.0-rc0

Type: Improvement Priority: Major - P3
Reporter: Kevin Pulo Assignee: Daniel Pasette (Inactive)
Resolution: Done Votes: 2
Labels: remove, usability
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to DOCS-2835 remove() without query no longer allowed Closed
Backwards Compatibility: Minor Change
Participants:

 Description   

The argument processing of db.coll.remove() seems to be modelled on db.coll.find(). However, the expected semantics of removal are substantially different to querying.

Semantically, the query parameter to find() is subtractive - as terms are added to the query, documents which don't have a matching form are elided from the set of documents returned. The query parameter is like a sieve, with an empty specification indicating no limitation on the form of documents to be returned. In the absence of any specification, defaulting to an "open sieve" and returning all documents is more likely to be useful than a "closed sieve" which returns nothing or throws an error.

However, I would argue that most users are used to remove/delete operations generally being semantically additive - only documents which match the query are added to the set of documents to be deleted. Given this, the default in the absence of a remove specification should be no documents, not all documents. Users expect this behavior because the highly destructive nature of removal/deletion means that conservative defaults are more prudent. The consequences of accidentally typing db.coll.find() are trivial, but the same cannot (currently) be said for db.coll.remove().

If users truly mean to remove all documents in a collection, it is not a significant imposition to require them to state this explicitly by running db.coll.remove({}). db.coll.remove() should not remove anything (nothing specified -> nothing removed).

The analogous behavior with Unix command line tools (which are notoriously unforgiving) is that /bin/ls defaults to *, but /bin/rm does not. If users actually want to delete all files in a directory, they have to do "rm *".



 Comments   
Comment by Githook User [ 05/Feb/14 ]

Author:

{u'username': u'monkey101', u'name': u'Dan Pasette', u'email': u'dan@10gen.com'}

Message: SERVER-12409 db.coll.remove() shell helper requires a query predicate
Branch: master
https://github.com/mongodb/mongo/commit/a56fff30fbe28b90ad3526fb285dec078be72979

Comment by A. Jesse Jiryu Davis [ 01/Feb/14 ]

If we make this change, remove() with no arguments should raise an error immediately: "a query is required, for example: remove({})".

Generated at Thu Feb 08 03:28:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.