wazzaday
wazzaday

Reputation: 9664

renaming a collection with mongoDB

I can not rename a collection in mongoDB. I can see that it exists and can write and read data from it. I have attempted the following using the node mongo native driver.

db.collection("mycollection").renameCollection("mynewcollection");

error: TypeError: Object #<Collection> has no method 'renameCollection'

and

db['mycollection'].renameCollection("mynewcollection");

Cannot call method 'renameCollection' of undefined

performing the following in the same place returns all docs as expected

db.collection("mycollection").find({}).toArray(function(err, docs){
    console.log(docs);
});

Upvotes: 7

Views: 5841

Answers (2)

user 12321
user 12321

Reputation: 2906

add this code to your collection.js (it should be added to line 153, or endof the collection.js code) , it should resolve the problem.

req.collection.rename(name, function(err, collection) {
     if (err) {
-      req.session.error('Something went wrong: ' + err);
+      req.session.error = 'Something went wrong: ' + err;
       console.error(err);
       return res.redirect('back');
     }

     req.updateCollections(req.db, req.dbName, function(err) {
       if (err) {
-        req.session.error('Something went wrong: ' + err);
+        req.session.error = 'Something went wrong: ' + err;
         return res.redirect('back');
       }

-      req.session.success('Collection renamed!');
+      req.session.success = 'Collection renamed!';
       res.redirect(config.site.baseUrl+'db/' + req.dbName + '/' + name);
     });
   });

Upvotes: 0

JohnnyHK
JohnnyHK

Reputation: 312149

The method to rename a collection using the node.js driver is rename, not renameCollection:

db.collection("mycollection").rename("mynewcollection", function(err, newColl) {...});

Upvotes: 22

Related Questions