【问题标题】:GeoJsonPoint in spring boot data mongodb throws errorsspring boot data mongodb中的GeoJsonPoint抛出错误
【发布时间】:2017-07-04 20:14:47
【问题描述】:

我遇到了 Spring Boot Data 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>

更新

@GeoSpatialIndex

默认为2d索引,为避免出现问题,必须将其设置为2dsphere-如果出现此问题,只需在注释中切换索引类型:

@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DS​​PHERE)

【问题讨论】:

    标签: java mongodb spring-boot


    【解决方案1】:

    关于this thread中的答案

    MongoDB 2d 索引需要传统的坐标对格式,它只是一个坐标数组,如 [1, 2]

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

    GeoSpatialIndexed 允许进行一些配置,参数 'name' 可能会有所帮助。

    【讨论】:

    • 我真的很想用这个 GeoJsonPoint 类型,我发现索引必须是 2dsphere 才能让它工作。
    猜你喜欢
    • 2019-05-16
    • 2017-06-09
    • 1970-01-01
    • 2015-12-17
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    相关资源
    最近更新 更多