Spring - mongoDb错误:com.mongodb.WriteConcernException:“索引无效的ns”

时间:2015-01-27 21:53:49

标签: java spring mongodb maven spring-mvc

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

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是com.mongodb.WriteConcernException:{“serverUsed”:“aValideMongoDB”,“err”:“无效的ns索引”,“代码”: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 *类必须改变一些导入,但没什么大不了的,我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

Spring驱动程序中存在一个错误如果我使用这个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

时开始失败