Skip to content

Aggregation query return a list of object with null value. #4895

@V0idk

Description

@V0idk

the mongo source code:

public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware, EnvironmentCapable {
    protected <S> S readDocument(ConversionContext context, Bson bson, TypeInformation<? extends S> typeHint) {
        Document var10000;
        if (bson instanceof BasicDBObject dbObject) {
            var10000 = new Document(dbObject);
        } else {
            var10000 = (Document)bson;
        }
        Document document = var10000;
        TypeInformation<? extends S> typeToRead = this.getTypeMapper().readType(document, typeHint);
        Class<? extends S> rawType = typeToRead.getType();
        if (this.conversions.hasCustomReadTarget(bson.getClass(), rawType)) {
            return this.doConvert(bson, rawType, typeHint.getType());
        } else if (Document.class.isAssignableFrom(rawType)) {
            return bson;
        } else if (DBObject.class.isAssignableFrom(rawType)) {
            if (bson instanceof DBObject) {
                return bson;
            } else if (bson instanceof Document) {
                Document doc = (Document)bson;
                return new BasicDBObject(doc);
            } else {
                return bson;
            }
        } else if (typeToRead.isMap()) {
            return context.convert(bson, typeToRead);
        } else if (BSON.isAssignableFrom(typeHint)) {
            return bson;
        } else {


public class AggregationResults<T> implements Iterable<T> {
    private final List<T> mappedResults;
    private final Document rawResults;
    @Nullable
    private final String serverUsed;
}

i have saw the mongo code have go to else branch. but rawResults is normal, and mappedResults is bad. I Don't know why?

Image

Image

i suspect that something wrong with MappingMongoConverter

    @Test
    void testAggregate() {
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("programming_language").is("Perl")),
                Aggregation.lookup("commits", "hash", "hash", "commits"),
                Aggregation.lookup("fixes", "commits.hash", "hash", "fixes"),
                Aggregation.lookup("cve", "fixes.cve_id", "cve_id", "cves"),
                Aggregation.unwind("cves")
        );

        // 执行聚合操作并返回结果
        List<Cve> cves = mongoTemplate.aggregate(aggregation, "file_change", Cve.class).getMappedResults();
        System.out.println();
    }

// =========

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document("cve")
public class Cve {

    @Id
    private ObjectId id;

    @Field("cve_id")
    private String cveId;

    @Field("published_date")
    private String publishedDate;
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.comstatus: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions