2015-11-03 426 views
3

我試圖插入包含文檔數組但是獲得以下異常的json字符串。使用Java將JSON文檔數組插入到MongoDB時出錯

MongoDB的服務器版本:3.0.6

蒙戈-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<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

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

<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> 
+0

應該每個貨運公司在不同的文件? – gidim

+0

不是一個大的MongoDB用戶,而是用RDBMS的術語來提及;每個貨運公司應該在不同的行中。 – user2325154

回答

4

您應該插入一個接一個,或創建一個文件列表,並使用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) 
} 
+0

完美:)工作! – user2325154

相關問題