Romeo
Romeo

Reputation: 1831

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

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?

Upvotes: 25

Views: 14653

Answers (4)

UdayKiran Pulipati
UdayKiran Pulipati

Reputation: 6667

Remove all collections from MongoDB database, use below Syntax:

db.getCollectionNames().forEach(
function(collection_name){
    print('CollectionName: '+ collection_name +', '+ db[collection_name].remove({})
)

});

Output:

CollectionName: Document_CODE, WriteResult({ "nRemoved" : 20 })
CollectionName: Document_DATA, WriteResult({ "nRemoved" : 10 })
CollectionName: Document_NAME, WriteResult({ "nRemoved" : 10 })
CollectionName: Document_COLLE, WriteResult({ "nRemoved" : 1 })

Upvotes: 0

Rizwan Siddiquee
Rizwan Siddiquee

Reputation: 891

Run:

db.collectionname.remove({});

result :

WriteResult({ "nRemoved" : 7 })

Upvotes: 1

Gergo Erdosi
Gergo Erdosi

Reputation: 42063

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.

Upvotes: 63

displayName
displayName

Reputation: 14389

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....

Upvotes: 2

Related Questions