Decoding '_id' errored with: readObjectId can only be called when CurrentBSONType is OBJECT_ID, not when CurrentBSONType is DOCUMENT

Hello,
I’m trying to group documents based on conditionGroupControlId, conditionGroupScac, conditionGroupId and then selecting the document having the highest conditionGroupRevisionId. Then putting the result in conditionGroups which is list of POJO PrismConditionGroup.

priceAggregate.add(new Document("$group",
              new Document("_id",
                      new Document("conditionGroupControlId", "$conditionGroupControlId")
                              .append("conditionGroupScac", "$conditionGroupScac")
                              .append("conditionGroupId", "$conditionGroupId"))
.append("conditionGroupRevisionId", new Document("$max", "$conditionGroupRevisionId"))));
 conditionGroupCollection.aggregate(priceAggregate).forEach(conditionGroups::add);

PrismConditionGroup extends another class containing below field:

  @BsonProperty("_id")
  @BsonId
  private ObjectId id;

Below is the error I’m getting:

[INFO] Decoding into a ‘PrismConditionGroup’ failed with the following exception:
[INFO]
[INFO] Failed to decode ‘PrismConditionGroup’. Decoding ‘_id’ errored with: readObjectId can only be called when CurrentBSONType is OBJECT_ID, not when CurrentBSONType is DOCUMENT.
[INFO]
[INFO] A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.

Can someone please suggest me how to solve this issue?

Hi @Aditi_Barde,

Welcome to the MongoDB Community forums :sparkles:

Can you add the annotation

@BsonProperty("id") 

to the field id, and it will work as expected!


If you have any doubts, please feel free to reach out to us.

Regards,
Kushagra Kesav

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.