Spring Data Mongo |无法授权插入没有字符串类型的 system.indexes 文档

Posted

技术标签:

【中文标题】Spring Data Mongo |无法授权插入没有字符串类型的 system.indexes 文档【英文标题】:Spring Data Mongo | Cannot authorize inserting into system.indexes documents without a string-typed 【发布时间】:2014-07-14 04:41:22 【问题描述】:

我在尝试创建 Spring Data Mongo 存储库时遇到问题。

相关代码&配置:-

模型类:-

@Document(collection = "USER_DETAIL")
public class UserDetail implements
        org.springframework.security.core.userdetails.UserDetails, Serializable 

    private static final long serialVersionUID = -2637223077307659181L;

    @Id
    private String id;

    @Indexed(dropDups = true, name = "usernameIndex", unique = true)
    private String username;

    private String fname;
    private String lname;
    private List<UserRole> authorities;
    private String password;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;

        // getter & setters.

Spring Data Repository 类:-

public interface UserDetailsRepository extends
        MongoRepository<UserDetail, String> 

    /**
     * Find a user by its username.
     * 
     * @param username
     * @return
     */
    public UserDetail findByUsername(String username);

Spring Java 配置:-

@Configuration
@EnableMongoRepositories(basePackages =  "x.y.z.repository" )
public class MongoRepositoryConfig 

    @Bean
    public MongoTemplate mongoTemplate() throws UnknownHostException 
        MongoTemplate mongoTemplate = new MongoTemplate(mongo(), "expunto");
        return mongoTemplate;
    

    @Bean
    public Mongo mongo() throws UnknownHostException 
        MongoClient client = new MongoClient("localhost");
        return client;
    


错误:- 无法授权插入没有字符串类型 \"ns\" 字段的 system.indexes 文档。","code":13,"n":0,"ok":1.0 在 com.mongodb.CommandResult.getException(CommandResult.java:77) ~[mongo-java-driver-2.11.4.jar:na]

堆栈跟踪:-

Caused by: com.mongodb.WriteConcernException:  "serverUsed" : "localhost/127.0.0.1:27017" , "connectionId" : 10 , "err" : "Cannot authorize inserting into system.indexes documents without a string-typed \"ns\" field." , "code" : 13 , "n" : 0 , "ok" : 1.0
    at com.mongodb.CommandResult.getException(CommandResult.java:77) ~[mongo-java-driver-2.11.4.jar:na]
    at com.mongodb.CommandResult.throwOnError(CommandResult.java:110) ~[mongo-java-driver-2.11.4.jar:na]
    at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:102) ~[mongo-java-driver-2.11.4.jar:na]
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:142) ~[mongo-java-driver-2.11.4.jar:na]
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:115) ~[mongo-java-driver-2.11.4.jar:na]
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:248) ~[mongo-java-driver-2.11.4.jar:na]
    at com.mongodb.DBApiLayer$MyCollection.createIndex(DBApiLayer.java:340) ~[mongo-java-driver-2.11.4.jar:na]
    at com.mongodb.DBCollection.createIndex(DBCollection.java:485) ~[mongo-java-driver-2.11.4.jar:na]
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:135) ~[spring-data-mongodb-1.5.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:129) ~[spring-data-mongodb-1.5.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:121) ~[spring-data-mongodb-1.5.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:105) ~[spring-data-mongodb-1.5.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:46) ~[spring-data-mongodb-1.5.0.RELEASE.jar:na]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98) ~[spring-context-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:333) ~[spring-context-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:307) ~[spring-data-commons-1.8.0.RELEASE.jar:na]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:181) ~[spring-data-commons-1.8.0.RELEASE.jar:na]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:141) ~[spring-data-commons-1.8.0.RELEASE.jar:na]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:67) ~[spring-data-commons-1.8.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getEntityInformation(MongoRepositoryFactory.java:141) ~[spring-data-mongodb-1.5.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getTargetRepository(MongoRepositoryFactory.java:83) ~[spring-data-mongodb-1.5.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158) ~[spring-data-commons-1.8.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224) ~[spring-data-commons-1.8.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210) ~[spring-data-commons-1.8.0.RELEASE.jar:na]
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:108) ~[spring-data-mongodb-1.5.0.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    ... 40 common frames omitted

更新:-

Spring Data Mongo 版本 - 1.5.0.RELEASE。 构建系统 - Maven

感谢您的帮助。

【问题讨论】:

MongoDB服务器是什么版本的? @chrylis - MongoDB 2.6 请同时发布您正在使用的 Spring Data 组件的版本,以及您如何管理构建(Maven/Gradle?)。 版本 - 1.5.0.RELEASE |用于构建的 Maven。 【参考方案1】:

您需要升级到 mongo-java-driver 2.12.1。

在 Spring Data MongoDB 1.5 中,他们从 collection.ensureIndex 切换到 collection.createIndex。 在 2.11.4 驱动程序中,这并没有将默认选项放在索引上,即“name”和“ns”选项。这已在 2.12.1 驱动程序中修复。

请参阅:https://jira.mongodb.org/browse/JAVA-1141。

【讨论】:

你拯救了我的一天。我在 2.4 版本的 mongo 上遇到了同样的问题,消息是“无效的 ns 到索引”。更新到 2.6 导致我到了那里。谢谢

以上是关于Spring Data Mongo |无法授权插入没有字符串类型的 system.indexes 文档的主要内容,如果未能解决你的问题,请参考以下文章

Spring Data Mongo Embedded 测试失败并出现“无法识别的管道阶段名称:'$graphLookup'”

将 mongo 查询转换为 spring-data-mongo 查询

如何使用 mongo 搜索集合并返回子文档列表(Spring-data-mongo)

插入嵌入文档而不阅读整个文档 - spring,mongo

使用 Spring data mongo 和 Spring data elasticsearch 时如何建模?

使用 mongoTemplate 在 spring-data-mongo Java 中进行 Mongo 聚合查询