Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB remove mapreduce collection

Due to error in client code, mongodb have created many "mr.mapreduce...." collections, how to remove them all (by mask maybe).

like image 979
INs Avatar asked Nov 12 '10 09:11

INs


People also ask

Does MongoDB use mapReduce?

Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. To perform map-reduce operations, MongoDB provides the mapReduce database command.

How do I delete multiple collections in MongoDB?

In MongoDB, you are allowed to delete the existing documents from the collection using db. collection. deleteMany() method. This method deletes multiple documents from the collection according to the filter.

Why mapReduce is discouraged in MongoDB?

The main reason for the weak performance of mongos map-reduce is that it's javascript engine SpiderMonkey.


2 Answers

I run script in interactive shell:

function f() {
    var names = db.getCollectionNames();
    for(var i = 0; i < names.length; i++){
    if(names[i].indexOf("mr.") == 0){
    db[names[i]].drop();}}};
f();

It resolved my problem.

like image 113
INs Avatar answered Sep 28 '22 10:09

INs


Temporary map-reduce table should be cleaned up when the connection which created them is closed:

map/reduce is invoked via a database command. The database creates a temporary collection to hold output of the operation. The collection is cleaned up when the client connection closes, or when explicitly dropped. Alternatively, one can specify a permanent output collection name. map and reduce functions are written in JavaScript and execute on the server.

-- MongoDB docs

If not, you could delete them using them same method you would delete any other collection. It might get a bit repetitive though.

like image 20
Mark Embling Avatar answered Sep 28 '22 10:09

Mark Embling