Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Mongo Basics Working With Collections Review: Managing Collections

Tiger Wang
Tiger Wang
26,548 Points

Can't run 'db.rename' on Mongodb

Hey guys:

It's about the Quiz, What command can be used to rename a database. And the answer goes to 'rename'. But I've tried every option in the list, included 'name' & 'mv'. The response is always :

TypeError: db.rename is not a function :
@(shell):1:1

There's no 'rename' function for db in Mongo's API Docs. I found this Jira post about 'rename database' ,here: Ability to rename databases

Should I use copyDatabase instead of rename it or any other better suggestion?

PS: Strongly suggesting to add a link about 'markdown syntax' on write new post/question/discuss.

Robert Komaromi
Robert Komaromi
11,927 Points

I had the same question! Also, if you look through the docs, you'll see there is no name/rename/mv database function.

Robert Komaromi
Robert Komaromi
11,927 Points

Thanks Ken. I just needed closure about this error - a quick Google search for 'how to rename a database in mongodb' gives me many answers. But double checking myself when I get an unexpected answer is a nice way to learn :p

Tiger Wang
Tiger Wang
26,548 Points

Thanks for both of you, guys. I already took 'copyDatabase' as the answer.

5 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Tiger;

You are correct! Nice catch, I missed it myself going through the course and in QA. :frowning: Renaming databases is not a straight forward process in MongoDB with a single command. Let's tag Huston Hedinger in on this so that he can adjust that quiz question. I wonder if the quiz question was meant to be how to rename a collection in MongoDB, in which case you can call the renameCollection() function.

Happy coding,
Ken

Dakota Bryant
Dakota Bryant
3,903 Points

Huston, it appears that the question is still in this course, with no reference to Mongo Hacker inside of the videos leading up. Is that intentional?

Just a reminder for QA. This broken question is still in the quiz for https://teamtreehouse.com/library/mongo-basics/working-with-collections/review-managing-collections

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Robert;

As I mentioned, renaming a database in MongoDB is, while not too complicated, a bit more effort that simply calling a rename function. The quickest method I have found to do so is:

db.copyDatabase(“db_to_rename”,”db_renamed”,”localhost”)

use db_to_rename

db.dropDatabase();

There are other solutions available, but for most situations I have found the above method to be efficient.

Ken

Huston Hedinger
STAFF
Huston Hedinger
Treehouse Guest Teacher

Hey guys - you are right - db.rename does not come packaged with the mongo shell. It does come with Mongo Hacker!

To tell you the truth, I did not realize that when I was creating the course and I apologize for the confusion.

In case you are interested, here is the mongo-hacker code that adds the rename method.

DB.prototype.rename = function(newName) {
    if(newName == this.getName() || newName.length === 0)
        return;

    this.copyDatabase(this.getName(), newName, "localhost");
    this.dropDatabase();
    db = this.getSiblingDB(newName);
};

As Ken Alger pointed out, it uses the copyDatabase and dropDatabase.