Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
});
like image 303
wazzaday Avatar asked Feb 02 '15 15:02

wazzaday


People also ask

How do I rename a collection in MongoDB?

In MongoDB, you can use the renameCollection() method to rename or change the name of an existing collection. The new name of the collection. Optional, if true then mongod drops the target of renameCollection former to renaming the collection. The default value is false.

How do I rename a collection name in MongoDB Atlas?

The renameCollection command renames a collection to the specified new name in the storage configuration. You can run this command only against the admin database, which is the Atlas user authentication database.

How do you rename a collection in MongoDB using Python?

The PyMongo function rename() is used to rename a collection. The rename operation fails if the new name is not an instance of basestring or it is an invalid collection name. Parameters : new_name : The new name of the collection.

Can I rename MongoDB database?

how can i update Database name and collection name ? db. copyDatabase('old_name', 'new_name'); use old_name db. dropDatabase();


1 Answers

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

db.collection("mycollection").rename("mynewcollection", function(err, newColl) {...});
like image 98
JohnnyHK Avatar answered Oct 19 '22 03:10

JohnnyHK