Dienstag, 16. Oktober 2012

Copying Data between Mongo Databases Using Javascript only

Copying Data between Mongo Databases Using Javascript only

MongoDB is a very popular noSql database with built in replication, horizontal scalability (sharding), Json as native data format and Javascript as the native query language. This blog post deals with copying data between mongo databases.

There are various methods for copying data between different MongoDB databases. The following possibilities come to my mind:
  • Using mongodump and mongorestore. This method is very fast and can be used to copy individual collections or the entire database from the command line.
  • Using mongoexport and mongoimport. This can be used to export data selected by a custom query or entire collections from the command line.
  • Using some programming language of your choice with a mongo driver.
  • Using the mongo shell and the use <database> or the db = connect("...")primitive.
The last solution is very convenient in that it can be used directly from Javascript, and gives you control such as counting the number of documents to copy. Below is an example use case:

> mongo localhost/test
PRIMARY> db.bla.find()
{ "_id" : ObjectId("507d19c4ce52c92d953fe8a1"), "1" : "one" }
{ "_id" : ObjectId("507d19cece52c92d953fe8a2"), "2" : "two" }
PRIMARY> var docs = db.bla.find()
PRIMARY> docs.size()
2
PRIMARY> use test2
switched to db test2
PRIMARY> while(docs.hasNext()) { db.blo.insert(docs.next()); }
PRIMARY> db.blo.find()
{ "_id" : ObjectId("507d19c4ce52c92d953fe8a1"), "1" : "one" }
{ "_id" : ObjectId("507d19cece52c92d953fe8a2"), "2" : "two" }
PRIMARY>

The above session shows how to interactively copy data between two databases on the same host. Note that this can also be done for databases on different machines or databases running on different ports  of the same machine. In this case you would need to switch the connection using db = connect("www.example.org:7777/mydb") or similar instead of the use primitive. 

Also note that you cannot use the iterator ( docs.find())  before the data is inserted into the target collection, otherwise you will loose data.

Keine Kommentare:

Kommentar veröffentlichen