I am struggling with importing data into Mongodb
from a Json
file.
I can do the same in command line by using mongoimport command
.
I explored and tried lot but not able to import from Json file using java.
sample.json
{ "test_id" : 1245362, "name" : "ganesh", "age" : "28", "Job" :
{"company name" : "company1", "designation" : "SSE" }
}
{ "test_id" : 254152, "name" : "Alex", "age" : "26", "Job" :
{"company name" : "company2", "designation" : "ML" }
}
Thank for your time. ~Ganesh~
Had a similar "problem" myself and ended up using Jackson with POJO databinding, and Morphia.
While this sound a bit like cracking a nut with a sledgehammer, it is actually very easy to use, robust and quite performant and easy to maintain code wise.
Small caveat: You need to map your
test_id
field to MongoDB's_id
if you want to reuse it.Step 1: Create an annotated bean
You need to hint Jackson how to map the data from a JSON file to a POJO. I shortened the class a bit for the sake of readability:
As for the embedded document
Job
, please have a look at the POJO data binding examples linked.Step 2: Map the POJO and create a datastore
Somewhere during your application initialization, you need to map the annotated POJO. Since you already should have a MongoClient, I am going to reuse that ;)
Do the actual importing
Now importing a given JSON file becomes as easy as
With a bit of refatcoring, this can be converted in a generic importer for Jackson annotated beans. Obviously, I left out some special cases, but this would out of the scope of this answer.
Suppose you can read the JSON string respectively. For example, you read the first JSON text
and assign it to a variable (String json1), the next step is to parse it,
put all dbo into a list,
then save them into database:
EDIT:
In the newest MongoDB Version you have to use Documents instead of DBObject, and the methods for adding the object look different now. Here's an updated example:
Imports are:
The code would like this (refering to the text above the EDIT):
you can also do it the way with the list. but then you need
But I think there is a problem with this solution. When you type:
in the mongo shell to get all objects in the collection, the result looks like the following:
which is not exactly the same as before.
With 3.2 driver, if you have a mongo collection and a collection of json documents e.g:
You can insert individually:
or bulk:
Runtime r = Runtime.getRuntime();
Process p = null;
--dir is the path to where your mongoimport is.
File dir=new File("C:\Program Files\MongoDB\Server\3.2\bin");
--this line will open your shell in giving dir, the command for import is exactly same as you use mongoimport in command promote
p = r.exec("c:\windows\system32\cmd.exe /c mongoimport --db mydb --collection student --type csv --file student.csv --headerline" ,null,dir);
I just faced this issue today and solved it in another different way while none here satisfied me, so enjoy my extra contribution. Performances are sufficient to export 30k documents and import them in my Springboot app for integration test cases (takes a few seconds).
First, the way your export your data in the first place matters. I wanted a file where each line contains 1 document that I can parse in my java app.
Then I get the file from my resources folder, parse it, extract lines, and process them with mongoTemplate. Could use a buffer.