当前位置:   article > 正文

mongodb java json bson_使用Java将JSON文档数组插入MongoDB时出错

org.bson.bsoninvalidoperationexception: readstartdocument can only be called

我试图插入一个包含文档数组的json字符串,但出现以下异常.

MongoDB服务器版本:3.0.6

Mongo-Java驱动程序版本:3.1.0

我知道insertOne()方法仅用于插入一个文档,但是在这里是一个文档数组.我不确定如何在这里使用insertMany()方法.

请指导.

我要插入的JSON字符串:

json = [{"freightCompanyId":201,"name":"USPS","price":8.00},{"freightCompanyId":202,"name":"FedEx","price":10.00},{"freightCompanyId":203,"name":"UPS","price":12.00},{"freightCompanyId":204,"name":"Other","price":15.00}]

异常日志:

Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is ARRAY.

at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:655)

at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:687)

at org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:421)

at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:138)

at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45)

at org.bson.Document.parse(Document.java:105)

at org.bson.Document.parse(Document.java:90)

at com.ebayenterprise.ecp.jobs.Main.insert(Main.java:52)

at com.ebayenterprise.ecp.jobs.Main.main(Main.java:31)

Main.java

public class Main {

private static final Logger LOG = Logger.getLogger(Main.class);

public static void main(String[] args) throws IOException {

String json = getAllFreightCompanies();

insert(json);

}

private static String getAllFreightCompanies() throws IOException {

FreightCompanyDao freightCompanyDao = new FreightCompanyDaoImpl(DataSourceFactory.getDataSource(DatabaseType.POSTGRES.name()));

List freightCompanies = freightCompanyDao.getAllFreightCompanies();

return GenericUtils.toJson(freightCompanies);

}

private static void insert(String json) {

MongoClient mongoClient = new MongoClient("GSI-547576", 27017);

MongoDatabase database = mongoClient.getDatabase("test");

MongoCollection table = database.getCollection("fc");

Document document = Document.parse(json);

table.insertOne(document);

}

}

GenericUtils.java

public final class GenericUtils {

private static final Logger LOG = Logger.getLogger(GenericUtils.class);

private GenericUtils() {

}

public static String toJson(List freightCompanies) throws IOException {

String json = new ObjectMapper().writer().writeValueAsString(freightCompanies);

LOG.debug("json = " + json);

return json;

}

}

pom.xml

org.mongodb

mongo-java-driver

3.1.0

jar

org.codehaus.jackson

jackson-mapper-asl

1.9.13

解决方法:

您应该一个一个地插入或者创建一个文档列表并使用insertMany()

这是一个例子:

MongoClient mongoClient = new MongoClient("GSI-547576", 27017);

MongoDatabase database = mongoClient.getDatabase("test");

MongoCollection < Document > table = database.getCollection("fc");

FreightCompanyDao freightCompanyDao = new FreightCompanyDaoImpl(DataSourceFactory.getDataSource(DatabaseType.POSTGRES.name()));

List < FreightCompany > freightCompanies = freightCompanyDao.getAllFreightCompanies();

for (FreightCompany company: freighetCompanies) {

Document doc = Document.parse(GenericUtils.toJson(company))

collection.insertOne(doc)

}

标签:mongodb,maven,json,java

来源: https://codeday.me/bug/20191027/1946659.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/140813
推荐阅读
相关标签
  

闽ICP备14008679号