且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何使用java从Json文件导入数据到Mongodb

更新时间:2022-11-02 21:29:59

假设你可以分别读取JSON字符串。例如,您读取第一个JSON文本

  {test_id:1245362,name:ganesh,age :28,Job:
{company name:company1,designation:SSE}
}
pre>

并将其分配给变量(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 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~

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.