2017-02-15 78 views
1

我遇到了Spring Boot Data MongoDB的問題。GeoJsonPoint在春季啓動數據mongodb拋出錯誤

我用dto在下面附上我的代碼。每次嘗試使用GeoJsonPoint對象插入新文檔時,我都會遇到com.mongodb.WriteConcernException: Write failed with error code 16804 and error message 'location object expected, location array not in correct format'異常。

import org.springframework.data.annotation.Id; 
import org.springframework.data.mongodb.core.geo.GeoJsonPoint; 
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed; 
import org.springframework.data.mongodb.core.mapping.Document; 

import java.util.Date; 

@Document(collection = "collection_2") 
public class SingleBusStop { 

    @Id 
    private String id; 

    @GeoSpatialIndexed 
    private GeoJsonPoint location; 

    private DayType dayType; 

    private String lineNumber; 

    //getters, setters 

} 

我在同一個項目中,我插入一些數據沒有問題(也GeoJsonPoint)得到了一些其他集合。出於某種原因,我無法將數據插入collection_2。我正在使用Mongo 3.4.2。我的pom.xml如下所示:

<groupId>pl.server.map</groupId> 
<artifactId>Utils</artifactId> 
<version>1.0-SNAPSHOT</version> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.0.RELEASE</version> 
</parent> 

<dependencies> 
    <dependency> 
     <groupId>commons-net</groupId> 
     <artifactId>commons-net</artifactId> 
     <version>3.5</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi-ooxml</artifactId> 
     <version>3.15</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-cache</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-mongodb</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>com.google.code.gson</groupId> 
     <artifactId>gson</artifactId> 
     <version>2.7</version> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>commons-io</groupId> 
     <artifactId>commons-io</artifactId> 
     <version>2.4</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-csv</artifactId> 
     <version>1.4</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-lang</groupId> 
     <artifactId>commons-lang</artifactId> 
     <version>2.6</version> 
    </dependency> 

</dependencies> 

UPDATE

@GeoSpatialIndex

是默認2d索引,以避免出現問題它必須被設置爲2dsphere - 在這種問題的情況下只需切換索引類型中註解:

@GeoSpatialIndexed(類型= GeoSpatialIndexType.GEO_2DS​​PHERE)

回答

1
關於

在應答this thread

MongoDB的2D索引需要遺留座標對格式,它僅僅是一個如[1,2]

0座標的數組

因此,您可能需要更改字段SingleBusStop.location的類型。

GeoSpatialIndexed允許一些配置,參數'名稱'可能會有所幫助。

+0

我真的很想使用這個GeoJsonPoint類型,我發現索引必須是2dsphere才能正常工作。 – Czolg