Import/Export

32 views
Skip to first unread message

Oliver

unread,
Sep 25, 2009, 1:00:27 PM9/25/09
to mongodb-user
What's the preferred way of copying one collection to another
database?

I tried with mongodump and mongorestore, but specifying the database
and/or collection with mongorestore doesn't help (I'm on 1.0).

pinocchio:~ oliver$ mongodump -d services -c suppliers -o tmp/dump
connected to: 127.0.0.1
DATABASE: services to tmp/dump/services
services.suppliers to tmp/dump/services/suppliers.bson
29603 objects
pinocchio:~ oliver$ mongorestore -d services_dev -c suppliers --dir
tmp/dump/
connected to: 127.0.0.1
tmp/dump/services/suppliers.bson
going into namespace [services.suppliers]
read 489874/11917784 bytes so far. (4%) 1028 objects
...
read 11605076/11917784 bytes so far. (97%) 28784 objects
29603 objects

Am I missing something, or is it just not the way dump/restore is
supposed to work?

Michael Dirolf

unread,
Sep 25, 2009, 1:04:21 PM9/25/09
to mongod...@googlegroups.com
restore has been changed in master to restore to the specified
database if you give a single database to restore.

so to move foo.test to bar.baz you could dump the foo database and
restore it as bar, and then do a renameCollection to move bar.test to
bar.baz (which is fast).

this is only available in the latest nightly.

- mike

Oliver

unread,
Sep 25, 2009, 1:09:56 PM9/25/09
to mongodb-user
I see. But restoring a complete database could override existing
collections, couldn't it?

Michael Dirolf

unread,
Sep 25, 2009, 1:12:13 PM9/25/09
to mongod...@googlegroups.com
restore only does inserts, so you'll never lose existing data in your
database. some of the inserts could fail though, say if you try
restoring and there is a unique index where some of the new documents
match the old ones.

Oliver

unread,
Sep 25, 2009, 1:25:01 PM9/25/09
to mongodb-user
Okay, I wasn't quite precise: Although it only does inserts, it could
add data to other collections in the target DB that I'd like to leave
untouched.

What I was really looking for is a dump/restore of a single collection
in DB1 to a target collection in DB2.

I presume I can easily write a script to do this, but I thought it'd
be possible with mongodump/mongorestore.

Michael Dirolf

unread,
Sep 25, 2009, 1:27:56 PM9/25/09
to mongod...@googlegroups.com
Ahh I see. Yes that is not currently possible: dump and restore
operate on the database level.

Feel free to open a jira for this.

Oliver

unread,
Sep 25, 2009, 1:39:05 PM9/25/09
to mongodb-user

Michael Dirolf

unread,
Sep 25, 2009, 1:44:46 PM9/25/09
to mongod...@googlegroups.com
Actually I just did a quick test and was wrong - with the latest
nightly this works as you'd hoped for. Specify a single collection to
dump and then just restore that one collection using the --db option
to change the db name. you can't change the collection name but a
renameCollection will take care of that.

Michael Dirolf

unread,
Sep 25, 2009, 1:49:23 PM9/25/09
to Michael Dirolf, mongod...@googlegroups.com
Here is a case for restoring to a different collection, although you
can probably work around this as described in my last message.

http://jira.mongodb.org/browse/SERVER-318

Oliver

unread,
Sep 25, 2009, 2:22:03 PM9/25/09
to mongodb-user
All of this is working just fine with the 2009-09-25 build.

Great.


On 25 Sep., 19:39, Oliver <oliver.eilh...@gmail.com> wrote:
> Filed underhttp://jira.mongodb.org/browse/SERVER-317

Michael Dirolf

unread,
Sep 30, 2009, 9:43:45 AM9/30/09
to Michael Dirolf, mongod...@googlegroups.com
for anybody tracking this issue - this has been resolved in master and the latest nightlies

Reply all
Reply to author
Forward
0 new messages