5

How can I migrate my MongoDB collections to RethinkDB tables?

I'm not concerned about changing my old Mongo _id's to Rethink id's because they will be ignored in my implementation, and I'm not concerned about them cluttering my data.

bobjones
  • 503
  • 2
  • 15

1 Answers1

14

I wrote a quick BASH script to solve this. Because I only had the JavaScript RethinkDB driver, I had to install the python driver first so I could use rethinkdb import.

For this example, I am migrating the mongo collections: users, pinboards, and analytics; add your collections as needed to the following BASH command:

for collection in users pinboards analytics; \
do \
  mongoexport \
    --host my.mongo.server \
    --db my_mongo_database \
    --collection $collection \
  > $collection.json; \
  rethinkdb import \
    --file $collection.json \
    --table my_rethink_database.$collection; \
  rm $collection.json; \
done

Don't forget to change the names of your collections, host, and database. Also, adjust arguments to mongoexport and rethinkdb import as needed.

This is quick and dirty, but I hope it gets someone started in the right direction!

bobjones
  • 503
  • 2
  • 15
  • Quick follow up: You can remove lingering `_id`'s (and `__v`'s if you used Mongoose) with [this recipe](http://rethinkdb.com/docs/cookbook/javascript/#removing-a-field-from-a-document). If I'm not mistaken, you can do this to an entire table by simply excluding the `.get("1")` method from the chain. – bobjones May 21 '15 at 16:57