Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-16024

[camel-mongodb] ChangeStreams Consumer cannot handle string based _id

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Later
    • 3.7.0
    • 3.x
    • camel-mongodb
    • None
    • Unknown

    Description

      We're having quite a basic use case: Our documents in MongoDB have string based IDs (field _id) - as opposed to having ObjectId() instances. It seems, the current release of camel-mongodb cannot handle that.

      Having defined a route based on ChangeStreams like that:

      from("mongodb:mongoConnection?database=db&consumerType=changeStreams&collection=coll")
        .routeId("changestream")
         .process("changeStreamProcessor")
         .to("stream:out");
      

      When an event arrives on an object with a string based _id; I get this Exception:

      Exception in thread "Camel (camel-1) thread #1 - mongodb://mongoConnection" org.bson.BsonInvalidOperationException: Value expected to be of type OBJECT_ID is of unexpected type STRING
      	at org.bson.BsonValue.throwIfInvalidType(BsonValue.java:419)
      	at org.bson.BsonValue.asObjectId(BsonValue.java:150)
      	at org.bson.BsonDocument.getObjectId(BsonDocument.java:271)
      	at org.apache.camel.component.mongodb.MongoDbChangeStreamsThread.doRun(MongoDbChangeStreamsThread.java:75)
      	at org.apache.camel.component.mongodb.MongoAbstractConsumerThread.run(MongoAbstractConsumerThread.java:68)
      	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
      	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
      	at java.base/java.lang.Thread.run(Thread.java:830)
      

      So we basically have a problem here: https://github.com/apache/camel/blob/camel-3.7.0/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbChangeStreamsThread.java#L75

      So now the fix would be easy - check the type with instanceof ObjectId and act accordingly - and i could also contribute that - but I'm not clear if that can succeed.

      As I see it, the _id is later set on the header as MongoDbConstants.MONGO_ID - which then also would be a string. IMHO that's totally fine, everybody knows what type their _id's have.

      How to resolve this? IMHO it's quite a normal use case to have non-ObjectId-IDs..

      Thanks for any input

      Attachments

        Activity

          People

            ramu11 Ramu
            dario.nuevo Dario Nuevo
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: