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?
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.
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()
. Theremove()
operation will delete documents individually & update indexes as the documents are deleted; adrop()
will immediately delete all documents & indexes for the collection.
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...
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With