我得到了错误信息。
在线程 "main "中出现异常 org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.
下面的代码我只想插入一个json文件。请你帮助我好吗?
public class hallo {
public static void main( String args[] ) {
// Creating a Mongo client
MongoClient mongo = new MongoClient( "localhost" , 27017 );
// Accessing the database
MongoDatabase database = mongo.getDatabase("dumpJsonFiles");
//System.out.println("Credentials ::"+ credential);
//Creating a collection
//database.createCollection("sampleCollection1");
System.out.println("Collection created successfully");
MongoCollection<Document> collection = database.getCollection("new_name");
System.out.println("Collection myCollection selected successfully");
Document document = new Document("title", "MongoDB")
.append("description", "database")
.append("likes", 100)
.append("url", "http://www.tutorialspoint.com/mongodb/")
.append("by", "tutorials point");
//Inserting document into the collection
collection.insertOne(document);
System.out.println("Document inserted successfully");
List<InsertOneModel<Document>> docs = new ArrayList<>();
int count = 0;
int batch = 100;
try (BufferedReader br = new BufferedReader(new FileReader("sample.json"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println("line is "+line);
docs.add(new InsertOneModel<>(Document.parse(line)));
count++;
if (count == batch) {
collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
docs.clear();
count = 0;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (count > 0) {
collection.bulkWrite(docs, new BulkWriteOptions().ordered(false));
}
//DBObject dbObject = (DBObject)JSON.parse("sample.json");
System.out.println("Document inserted 2 successfully");
}
}
输入的json文件示例内容(从注释中复制)。
{
"Details":{
"Name":"AC",
"Rollno":"5978",
"section":"",
"Maths":"10",
"Science":"20",
"social":"20",
"English":"30"
}
}
另一个json文件是:
[
{
"Name":"Table",
"Description":"Used",
"Marks":[
{
"Social":"10",
"Science":"20",
"Maths":"30",
"English":"40",
"Hindi":"50",
"Computers":[
{
"Lab":"10",
"Theory":"20"
}
]
}
]
}
]
你得到的错误是因为输入的JSON数据文件有问题。很可能是文件中的JSON文件的格式是 阵列;例如一个有两个文件的文件。
[{ "type": "fruits", "items": [ "apple", "orange"] },
{ "type": "objects", "item": { "property": "color", "value": "green" } }]
如果你运行你的代码(你的Java代码工作正常),就会出现和你一样的错误。
Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSON Type is DOCUMENT, not when CurrentBSONType is ARRAY...。
正确的格式化方式是如下所示的两行,每行代表一个JSON。
{ "type": "fruits", "items": [ "apple", "orange"] }
{ "type": "objects", "item": { "property": "color", "value": "green" } }
这样的结果是在集合中出现了两个文件。
{
"_id" : ObjectId("5ee06cfe7bd8631234d4c283"),
"type" : "fruits",
"items" : [
"apple",
"orange"
]
}
{
"_id" : ObjectId("5ee06cfe7bd8631234d4c284"),
"type" : "objects",
"item" : {
"property" : "color",
"value" : "green"
}
}
NOTES。
MongoClient
正在使用 MongoClients.create()
方法。MongoClient
对象后,它与MongoClint#close
方法 - 释放资源。参见例子.