0

I have a BSON export from mongodump, and I also have a JSON export for mongoexport

What would be the easiest way to import with mgo? Does mgo support inserting a backed-up BSON collection?

Or do I need to use the JSON export, unmarshal it and then do insert() with mgo?

Thing is that I don't want to have to specify a scheme in my Go file - I just want to dump the file into a database.

K2xL
  • 9,730
  • 18
  • 64
  • 101

1 Answers1

1

What would be the easiest way to import with mgo?

Easiest? Shell out to mongorestore from your go program. Boom, done.

Does mgo support inserting a backed-up BSON collection?

I don't see any first-class support for it. (You could email the author). It should be possible, but it may be a bit of work. You should be able to use the mgo BSON layer to load the *.bson files and insert them into the DB. But you'll also have to parse the *.metadata.json files for the indexes, etc. It seems like a lot of work. (basically rewriting mongorestore.)

Or do I need to use the JSON export, unmarshal it and then do insert() with mgo?

That would be slower, and you'd have to test that $date and $oid are handled correctly, but it seems like it should work. It might even be simpler to write because you don't have to learn the BSON layer.

BraveNewCurrency
  • 12,654
  • 2
  • 42
  • 50
  • So I am actually shelling out right now, but Im not the biggest fan of that solution since it sort of depends on that executable existing on the same box as the go program (unless I ssh to a box with it installed which I would rather not do). Thanks for the detailed response! – K2xL Apr 06 '14 at 23:00
  • One more thing: You could also stop mongo, and zip up the raw DB files. Then you can unzip them in pure go without shelling out. (You'd still have to stop mongo and restart it, but you may be doing that in go already.) – BraveNewCurrency Apr 06 '14 at 23:18
  • I'm trying for the third approach, but man I can't figure out how to unmarshal an unstructure json. I just want to insert the JSON with mgo :-( – K2xL Apr 06 '14 at 23:22
  • This may help: http://stackoverflow.com/questions/21187057/how-do-i-deal-with-an-arbitrary-hash-returned-from-mongo-in-go-using-mgo – BraveNewCurrency Apr 06 '14 at 23:26
  • Thanks. Sigh. Still not getting any records loaded in. http://pastebin.com/LbdUmJ3v Thoughts? – K2xL Apr 06 '14 at 23:31
  • (well, it runs, but I'm not sure I'm doing it right) – BraveNewCurrency Apr 07 '14 at 00:41
  • I actually don't remember Edwin, I'm sorry :( if it comes back to me on what I ended up doing I'll post – K2xL Dec 21 '17 at 13:36