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);
});
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.
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.
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.
how can i update Database name and collection name ? db. copyDatabase('old_name', 'new_name'); use old_name db. dropDatabase();
The method to rename a collection using the node.js driver is rename
, not renameCollection
:
db.collection("mycollection").rename("mynewcollection", function(err, newColl) {...});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With