【问题标题】:Spring - mongoDb Error: com.mongodb.WriteConcernException: "invalid ns to index"Spring - mongoDb 错误:com.mongodb.WriteConcernException:“无效的 ns 索引”
【发布时间】:2015-01-27 21:53:49
【问题描述】:

我有一个使用 2 个数据源的 Spring 4 应用程序,其中一个是 MongoDB。当我进行近距离查询时,我收到以下错误:

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 com.mongodb.WriteConcernException: { "serverUsed" : "aValideMongoDB" , "err" : "invalid ns to index" , "code" : 10096 , "n" : 0 , "connectionId" : 955 , "ok" : 1.0}

失败的代码:

private List<GeoSearchDealer> doDealerQuery(NearQuery nearQuery, DistanceKind distanceKind) {
    // FAILS AT THIS LINE
    GeoResults<InventoryDealer> results = mongoTemplate.geoNear(nearQuery, InventoryDealer.class);
    List<GeoSearchDealer> dealers = new ArrayList<GeoSearchDealer>();

    for (GeoResult<InventoryDealer> result : results) {
        ...
    }

    return dealers;
}

一切都是以编程方式创建的,所以没有 XML。这是配置类:

@Configuration 
@ComponentScan("path.to.code.base") 
@EnableWebMvc
@EnableTransactionManagement
@EnableMongoRepositories("path.to.code.base")
@EnableSpringDataWebSupport
@EnableMongoAuditing
public class AppConfig {

    // other beans omitted for brevity

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        MongoClient mongoClient = new MongoClient(mongoHost);
        return new SimpleMongoDbFactory(mongoClient, mongoDb);
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        return mongoTemplate;
    }
}

InventoryDe​​aler 类:

@Document(collection="inventory_dealer")
public class InventoryDealer {
    ...
}

pom.xml:

    <spring.version>4.0.6.RELEASE</spring.version>
    ...
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>2.11.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.6.1.RELEASE</version>
    </dependency>
    ...

此代码是从一个正在运行但正在使用的原型应用程序移植的

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>2.11.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>

因此,某些 Geo* 类必须更改一些导入,但没有什么大不了的,我在这里做错了什么?

【问题讨论】:

  • 我会在一天左右的等待期后回答我自己的问题,但本质上,java mongo 驱动程序的 2.11.3 版本存在一个错误,如果你使用最新版本,你可以仅使用 spring-data-mongodb 库的 1.4.1 或更早版本。
  • 我有一个建议要研究一下,我使用 mongo 但不使用 Spring,所以请谨慎对待 - 我只提到它,因为你提到了你正在做的事情的变化geo 和一个提到索引的错误 - mongo 在允许的地理索引中发生了一些变化 - 可能有索引可能会给你写错误 - 例如将无效的 GeoJson 对象写入 2dsphere 索引。检查是否可以直接在 shell 中执行相同的写入操作 - 如果是非法写入,shell 将显示相同的错误。
  • @SteveB 问题如前所述 - 存在驱动程序问题。当我将它移植为原型工作时,我知道该查询有效,但使用的是旧版本的库。我将所有内容都移植到了最新版本,但一切都停止了工作。我去了一个旧版本,它开始工作了。爆炸的是 java-mongo-driver 2.11.3 和 spring-data-mongodb 1.6.1 的组合。将任一版本移回并开始工作。我选择回到 java-mongo-driver 2.11.0

标签: java spring mongodb maven spring-mvc


【解决方案1】:

Spring Driver 中有一个 bug 如果我使用这个 pom.xml 配置,它适用于最新的 mongo:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <!-- <version>2.11.3</version> -->
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.6.1.RELEASE</version>
    <!-- <version>1.4.1.RELEASE</version> -->
</dependency>

但如果我使用版本 2.11.3,它会失败并出现下面列出的错误。

驱动程序 2.11.3 适用于 1.2.0 - 1.4.1,但开始使用 1.5.1 失败

【讨论】:

    猜你喜欢
    • 2017-01-12
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2013-06-09
    • 2017-01-06
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多