Mongodb删除_class 属性

今天我做了一个查询数据典的功能,,,,,使用了mongorepository,,出现了一个很奇怪的现象

emmm 怎么说,真的是一个大坑 数据怎么查都查不出来..但是我调用 findAll(), findByParam()的时候又能查询出来

  public SysDictionary selectDictionaryByType(String dType) {
        if (StringUtils.isBlank(dType)){
            ExceptionCast.cast(CommonCode.INVALIDPARAM);
        }


        List<SysDictionary> result1 = sysDictionaryRepository.findAll();

        List<SysDictionary> result2 = sysDictionaryRepository.findByDType(dType);



        ExampleMatcher matching = ExampleMatcher.matching();//匹配器



//        条件封装
        SysDictionary sysDictionary = new SysDictionary();

        sysDictionary.setDType(dType);


        Example<SysDictionary> ex = Example.of(sysDictionary,matching);

        List<SysDictionary> all = sysDictionaryRepository.findAll(ex);


        if (all != null & all.size() > 0){
            return all.get(0);
        }

        return null;
    }

下面是我的运行结果,,,,

这个 bug 郁闷的-  - 

然后我用debug 看了一下查询语句

这是result1 的查询语句

这是result2的查询语句 

然后这是 all的查询语句 

这属性真的是坑来着.......我网上找了挺久的,终于找到怎么删除的方法了  ,我们找到MongoConfig类 

package com.xuecheng.manage_course.config;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

@Configuration
public class MongoConfig {

    String db = "sysconfig";   //数据库名 
// 连接数据库的配置 
    @Bean
    public GridFSBucket getGridFSBucket(MongoClient mongoClient){
        MongoDatabase database = mongoClient.getDatabase(db);
        GridFSBucket bucket = GridFSBuckets.create(database);
        return bucket;
    }


//删除_class 属性的配置 
    @Bean
    public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
        try {//
            mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
        } catch (NoSuchBeanDefinitionException ignore) {
        }

        // Don't save _class to mongo
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return mappingConverter;
    }
}

然后配置好后运行看结果 

猜你喜欢

转载自blog.csdn.net/qq_40794266/article/details/84950185