我如何使用java从Json文件导入数据到Mongodb [英] How can I import data to Mongodb from Json file using java
问题描述
我在从 Json
文件中将数据导入 Mongodb
时遇到困难。
可以通过使用 mongoimport命令
在命令行中执行相同操作。
我使用java浏览并尝试很多,但无法从Json文件导入。 p>
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}
}
感谢您的时间。
〜Ganesh〜
假设你可以分别读取JSON字符串。例如,您读取第一个JSON文本
{test_id:1245362,name:ganesh,age :28,Job:
pre>
{company name:company1,designation:SSE}
}
并将其分配给变量(String json1),下一步是解析它,
DBObject dbo =(DBObject)com.mongodb.util.JSON.parse(json1);
将 dbo 全部放入列表中,
List< DBObject> list = new ArrayList<>();
list.add(dbo);
,然后将它们保存到数据库中:
new MongoClient()。getDB(test)。getCollection(collection)。
编辑:
在最新的MongoDB您必须使用文档而不是DBObject,并且现在添加对象的方法看起来不同。这是一个更新的示例:
导入是:
import com。 mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
代码会这样(参考EDIT上面的文本):
文档doc = Document.parse(json1);
new MongoClient()。getDataBase(db)。getCollection(collection)。insertOne(doc);
你也可以用列表的方式。但是你需要
new MongoClient()。getDataBase(db)。getCollection(collection)。insertMany );
但我认为这个解决方案有一个问题。当您输入:
db.collection.find()
在mongo shell中获取集合中的所有对象,结果如下所示:
{_id:ObjectId(56a0d2ddbc7c512984be5d97),
test_id:1245362,name:ganesh,age:28,Job:
{company name:company1,designation:SSE
}
}
这和以前不一样。
I am struggling with importing data into
Mongodb
from aJson
file.
I can do the same in command line by usingmongoimport 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~
解决方案Suppose you can read the JSON string respectively. For example, you read the first JSON text
{ "test_id" : 1245362, "name" : "ganesh", "age" : "28", "Job" : {"company name" : "company1", "designation" : "SSE" } }
and assign it to a variable (String json1), the next step is to parse it,
DBObject dbo = (DBObject) com.mongodb.util.JSON.parse(json1);
put all dbo into a list,
List<DBObject> list = new ArrayList<>(); list.add(dbo);
then save them into database:
new MongoClient().getDB("test").getCollection("collection").insert(list);
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:
import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; import org.bson.Document;
The code would like this (refering to the text above the EDIT):
Document doc = Document.parse(json1); new MongoClient().getDataBase("db").getCollection("collection").insertOne(doc);
you can also do it the way with the list. but then you need
new MongoClient().getDataBase("db").getCollection("collection").insertMany(list);
But I think there is a problem with this solution. When you type:
db.collection.find()
in the mongo shell to get all objects in the collection, the result looks like the following:
{ "_id" : ObjectId("56a0d2ddbc7c512984be5d97"), "test_id" : 1245362, "name" : "ganesh", "age" : "28", "Job" : { "company name" : "company1", "designation" : "SSE" } }
which is not exactly the same as before.
这篇关于我如何使用java从Json文件导入数据到Mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!