java.lang.ClassCastException 在 ArrayList<Document> 的 collect() 上

java.lang.ClassCastException on collect() of ArrayList<Document>

我正在尝试搜索 mongo 数据库以在以下字段之一中查找用户给定的搜索词:

    @GetMapping(value = {"/classes/search/{term}"})
    public String className (
        @PathVariable(required = true, value = "term") String term
    ) throws Exception {
        // Escape user input
        final String filteredTerm = escapeUserInput(term);

        // Match using Bson filter
        ArrayList<Document> matchedClasses = new ArrayList<Document>();
        for (String modName : DATABASE.listCollectionNames()) {
            ArrayList<Document> filteredContents = new ArrayList<Document>();
            try {
                getCollection(modName).find(
                    Filters.or(
                        Filters.text(filteredTerm),
                        Filters.eq("count", Long.parseLong(filteredTerm))
                    )
                ).into(filteredContents);
            } catch (NumberFormatException e) {
                getCollection(modName).find(Filters.text(filteredTerm)).into(filteredContents);
            }

            // Add to final doc if match is found
            if (filteredContents != null) {
                matchedClasses.addAll(filteredContents);
            }
        }

        return String.valueOf(matchedClasses
            .stream()
            .distinct()
            .sorted()
            .collect(Collectors.toList())
        );

    }

}

getCollection() 所做的只是检索 mongo 集合。

当传递任何与数据库中的内容成功匹配的搜索词时,我在第 140 行得到一个 ClassCastException (.collect(Collectors.toList()))

java.lang.ClassCastException: class org.bson.Document cannot be cast to class java.lang.Comparable (org.bson.Document is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
    at java.base/java.util.Comparators$NaturalOrderComparator.compare(Comparators.java:47) ~[na:na]
    at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:na]
    at java.base/java.util.TimSort.sort(TimSort.java:220) ~[na:na]
    at java.base/java.util.Arrays.sort(Arrays.java:1515) ~[na:na]
    at java.base/java.util.ArrayList.sort(ArrayList.java:1750) ~[na:na]
    at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:392) ~[na:na]
    at java.base/java.util.stream.DistinctOps.end(DistinctOps.java:168) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
    at com.api.main.Executer.className(Executer.java:140) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
<TRUNCATED>

我怀疑这与 Filters.text 有关,因为那是我更改的内容,它已开始导致此错误,但我不明白为什么以及我下一步的下一步是什么?

matchedClasses 是一个 ArrayList<Document>Document 没有实现 Comparable.

当 运行 .collect(Collectors.toList()) 时,它还会尝试对 Stream 元素进行排序,因为您调用了 sorted。用于排序的机器人,元素需要实现 Comparable 或将 Comparator 传递给 .sorted().

由于Document是一个你无法控制的MongoDBclass,你需要传递一个Compatator.sorted():

//...
.sorted((document1,document2)->/*compare documents here*/)
//...