简体   繁体   English

使用Java将JSON文档数组插入MongoDB时出错

[英]Error while inserting an array of JSON documents in to MongoDB using Java

I am trying to insert a json string which contains an array of documents but getting following exception. 我试图插入一个json字符串,其中包含一个文档数组,但得到以下异常。

MongoDB server version: 3.0.6 MongoDB服务器版本: 3.0.6

Mongo-Java driver version: 3.1.0 Mongo-Java驱动程序版本: 3.1.0

I understand that insertOne() method is used to insert just one document but over here it's an array of documents. 我知道insertOne()方法只用于插入一个文档,但在这里它是一个文档数组。 I am not sure how to use insertMany() method here. 我不知道如何在这里使用insertMany()方法。

Please guide. 请指导。

JSON String that I want to insert: 我要插入的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 Log: 异常日志:

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 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<FreightCompany> 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<Document> table = database.getCollection("fc");
        Document document = Document.parse(json);
        table.insertOne(document);
    }

}

GenericUtils.java GenericUtils.java

public final class GenericUtils {

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

    private GenericUtils() {
    }

    public static String toJson(List<FreightCompany> freightCompanies) throws IOException {
        String json = new ObjectMapper().writer().writeValueAsString(freightCompanies);
        LOG.debug("json = " + json);
        return json;
    }

}

pom.xml 的pom.xml

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.1.0</version>
    <type>jar</type>
</dependency>
<dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-asl</artifactId>
   <version>1.9.13</version>
</dependency>

You should either insert one by one or create a List of documents and use insertMany() 您应该逐个插入或创建文档列表并使用insertMany()

Here's an example: 这是一个例子:

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM