Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove needs a query at src/mongo/shell/collection.js

Tags:

Getting this error when I run db.messages.remove(). "remove needs a query at src/mongo/shell/collection.js". any suggestions to resolve this error?

like image 377
Romeo Avatar asked Aug 03 '14 11:08

Romeo


People also ask

How do I remove all data from a collection in MongoDB?

To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.


2 Answers

As the message says you need to provide a query, but it can be an empty one (if you want to remove all documents):

db.messages.remove({}) 

EDIT: I would like emphasize the comment made by Stennie:

Note: if you actually want to remove all documents in a collection it is faster to a do a collection.drop(). The remove() operation will delete documents individually & update indexes as the documents are deleted; a drop() will immediately delete all documents & indexes for the collection.

like image 65
Gergo Erdosi Avatar answered Oct 07 '22 22:10

Gergo Erdosi


If you are trying to drop the collection messages, then you need to execute db.messages.drop().

Otherwise, the command db.messages.remove() is used to delete a particular document and therefore you need to write a query to allow MongoDB engine to know which document needs to be gotten rid of. For ex. db.messages.remove({_id : }).

The lack of {_id : } is causing the error that remove needs a query....

like image 44
displayName Avatar answered Oct 07 '22 23:10

displayName