db.collection.delete[One|Many)() vs db.collection.remove()

2,336 views
Skip to first unread message

SeungUck Lee

unread,
Aug 30, 2016, 8:43:03 AM8/30/16
to mongodb-user
Hi.

We should specify shard key condition to use db.collection.remove() operation on sharded cluster.
But not for db.collection.deleteMany() operation.


Delete Behavior
Indexes
Delete operations do not drop indexes, even if deleting all documents from a collection.

Does "drop indexes" of this paragraph mean db.collection.dropIndex() ? or delete() does not remove entry of index which is belong to documents which will be removed ?
I think delete() operation is kind of DML not DDL. I don't understand why manual note something about DDL(drop indexes) in page of DML(delete()). 
It's confusing to me.




The remove() method uses the delete command, which uses the default write concern. To specify a different write concern, include the write concern in the options parameter.

According to the manual, Looks like both of remove() and deleteMany() are use same internal command.
Then why does delete[One | Many]() method is introduced in MongoDB 3.2 ?
I am wondering that there's some reason MongoDB make new method (deleteOne, deleteMany) beside of remove().

Regards,
Matt.






Stephen Steneker

unread,
Aug 30, 2016, 9:53:32 AM8/30/16
to mongodb-user
On Tuesday, 30 August 2016 22:43:03 UTC+10, SeungUck Lee wrote:
We should specify shard key condition to use db.collection.remove() operation on sharded cluster. 
But not for db.collection.deleteMany() operation.

Hi,

If you want to remove specific documents from a collection you would provide a query with the criteria for matching documents to delete. The query criteria doesn't have to match the shard key, but ideally should be supported by an index to make it efficient to find matching documents.
 


Delete Behavior
Indexes
Delete operations do not drop indexes, even if deleting all documents from a collection.

Does "drop indexes" of this paragraph mean db.collection.dropIndex() ? or delete() does not remove entry of index which is belong to documents which will be removed ?

The description here is meant to suggest that removing individual documents does not remove index definitions for a collection. For example, if you remove() all documents from a collection you will end up with an empty collection with the indexes still defined (but no index entries).

If you want to completely remove a collection (all documents and index definitions) you can use db.collection.drop().


I think delete() operation is kind of DML not DDL. I don't understand why manual note something about DDL(drop indexes) in page of DML(delete()).  
It's confusing to me.

You are correct .. deleting documents does not change collection-level options like index definitions. I expect the documentation mentions this here because some users have conflated deleting all documents with dropping the collection.
 


The remove() method uses the delete command, which uses the default write concern. To specify a different write concern, include the write concern in the options parameter.

According to the manual, Looks like both of remove() and deleteMany() are use same internal command.
Then why does delete[One | Many]() method is introduced in MongoDB 3.2 ?
I am wondering that there's some reason MongoDB make new method (deleteOne, deleteMany) beside of remove().

The new methods were added to make the mongo shell more consistent with the driver APIs; the old methods remain for backwards compatibility. For more background, see: The MongoDB Shell's New CRUD API.

Regards,
Stephen

SeungUck Lee

unread,
Aug 30, 2016, 11:05:37 AM8/30/16
to mongodb-user
Hi Stephen.

Thanks for your clarifications.

Regards,
Matt.
Reply all
Reply to author
Forward
0 new messages