Craig
Craig

Reputation: 540

Importing a MongoDb Collection through Ruby (ROR)

I'm trying to seed my database with a collection exported via the mongoexport tool, but I can't seem to find any way to use the mongoimport tool through Ruby.

I looked at the Mongo Driver for how to execute mongo queries via Ruby, and thought about iterating through each line of json from the export, but there are keys like "$oid" which give errors when attempting to do a collection.insert()

Is it possible to use the mongoimport tool in Ruby, or what's the best way to add code to seeds.rb so that it imports a mongo collection?

Upvotes: 0

Views: 1373

Answers (2)

Kyle Banker
Kyle Banker

Reputation: 4359

mongoexport exports documents in an extended json format specified in the MongoDB docs.

http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON

The driver doesn't read this format automatically. For seeding a database, you may want to use mongodump and mongorestore, which use the database's native BSON format. As another poster mentioned, you could easily shell out to do this.

Upvotes: 0

Gates VP
Gates VP

Reputation: 45297

The mongoimport tool is actually a command-line tool. So you don't use the Mongo Driver for this.

Instead you should "shell out" and call the process. Here's a link on calling a command from the shell.

Calling shell commands from Ruby

Upvotes: 1

Related Questions