Is there a way to delete objects from multiple collections? Like delete everything in this 5 collections command in MongoDB?
It is kind of time consuming to go and manually delete from each collection in a Mongo database.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As Andrew mentioned, there is no built in way to do this, but you can use Javascript to get what you want. For example, if I wanted to drop a list of databases quickly (and dirtily, this is just a proof-of-concept code sample!):
No error checking of course, but this kind of server side script will work for cross-collection and cross-database functionality. If you wanted to run something on all your databases, then you could construct the list like this:
Of course, you need to be very, very careful when running things like this so you avoid destroying your database, so test extensively and use judiciously 🙂
For more on server side JS, check out the MongoDB docs here:
http://www.mongodb.org/display/DOCS/Server-side+Code+Execution