Laravel-mongodb: Truncate should only delete the documents not drop the collection

Created on 19 Sep 2017  路  3Comments  路  Source: jenssegers/laravel-mongodb

Using truncate() on model removes all the documents and indexes where as it should only delete the documents.

Here's what i used

ModelName::truncate();
bug

All 3 comments

Actually there is no truncate method for MongoDB. There are two options, drop() or remove({}). First drops all documents and indexes but it is fast, the second one removes all documents in collection in a loop, it is slow, it doesn't free up the spaces used for documents and indexes.

Eloquent method truncate() is referring to drop() method on official mongodb/mongodb package.

If you want to just remove documents, i think something like ModelName::delete(); will work.

Ref: https://stackoverflow.com/questions/16493902/truncate-a-collection

@ysb yes but the library has a method truncate which should logically truncate the data than dropping the collection. Also here's the commit that fixed it, but I don't know why they reverted the change.

I believe that Collection::truncate() should use remove({}) for greater clarity and compatibility with Laravel. The speed of execution in this case is not so important.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

geofflancaster picture geofflancaster  路  3Comments

tomartailored picture tomartailored  路  3Comments

YSimple picture YSimple  路  3Comments

imrannazirbhat picture imrannazirbhat  路  3Comments

naveedyasin picture naveedyasin  路  3Comments