org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘testApplicat

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘testApplication’: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.nos.test.TestApplication$$EnhancerBySpringCGLIB$$c1828724]: Constructor threw exception; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Invalid numeric type, found: STRING; nested exception is org.bson.BsonInvalidOperationException: Invalid numeric type, found: STRING

Question request: List all the course names that appear in the STUDENT collection (distinct)

 

 public List<String> test6_01(){
        Query query = new Query();
        List<Integer> list_course = mongoTemplate.findDistinct(query,"CID","student_course",Integer.class);
        System.out.println(list_course);
        List<String> list = new ArrayList<>();
        for(Integer i:list_course){
            Query query1 = new Query(Criteria.where("CID").is(i));
            Course course = mongoTemplate.findOne(query1,Course.class,"course");
            list.add(course.getNAME());
        }
        return list;
    }

Org.bson.bsoninvalidoperationexception: invalid numeric type, found: String prompts me to find the wrong data type. Only when the CID found is of string type can I find the data imported in the data. The CID is automatically set to string type.

Then let’s look at the specific usage of finddistinct() in mongodb: you can re query, but you can’t sort.

The method receives 4 or 5 parameters, respectively:

1 query criteria
2 fields to be de duplicated filed
3 collection database table to be queried
4 POJO instance corresponding to collection to be queried
5 return result class (return list of filed fields to be de duplicated)

After modification:

  public List<String> test6_01(){
        Query query = new Query();
        List<String> list_course = mongoTemplate.findDistinct(query,"CID","student_course",String.class);
        System.out.println(list_course);
        List<String> list = new ArrayList<>();
        for(String i:list_course){
            Query query1 = new Query(Criteria.where("CID").is(i));
            Course course = mongoTemplate.findOne(query1,Course.class,"course");
            list.add(course.getNAME());
        }
        return list;
    }

Query results:

Read More: