Kamilski81
Kamilski81

Reputation: 15107

How do I batch insert in MongoDB?

I have 40,000 lines that I wish to insert into a mongo database but I'm not sure the best way to do this.

Would I use the command line 'mongo' and paste it (I've been messing with this idea and it doesn't seem like it is ideal)

Are there any optimal or best practices here? Should I just do it in mongoid instead of mongo?

Upvotes: 0

Views: 303

Answers (1)

Ram Rajamony
Ram Rajamony

Reputation: 1723

Ugh. I posted an answer here and then realized there is a command called mongoimport that can directly write the DB files.

Lets assume you have your data in JSON format, one object per line, like so:

{"a":"a1"}
{"a":"a2"}
{"a":"a3"}

You would then do:

mongoimport --dbpath PATH_TO_YOUR_DB_DIR -d DB_NAME -c COLLECTION_NAME --file JSON_FILE_NAME

The use of dbpath needs to lock the data directory, so it cannot be used if a mongod is currently accessing the same path. mongoimport --help gives you pretty useful info.

Upvotes: 1

Related Questions