Java Driver 3.6.0: Exception on watching ChangeStreams

892 views
Skip to first unread message

Tobi Nonymous

unread,
Jan 17, 2018, 5:12:43 PM1/17/18
to mongodb-user
Hi,

I want to try out getting a $changeStream of inserts into a collection, 
but when I try to run the following simple code an "IllegalStateException: Invalid Map type. Map must have two type parameters defined." is thrown (see end of post for stacktrace):

MongoClient mongoClient = MongoClients.create(new ConnectionString("mongodb://localhost:27017"));
MongoDatabase database = mongoClient.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("some-collection");

collection.watch(asList(
                Aggregates.match(and(asList(
                        in("operationType", asList("insert")),
                        eq("fullDocument.even", 1L)))
                )));

I followed this short tutorial (https://blog.codecentric.de/en/2018/01/change-streams-mongodb-3-6/), so I guess normally this code should be working 
I am not sure what the problem is, I have tried the mongodb-driver and mongodb-driver-async libraries in version 3.6.0 and 3.6.1.

I also have included spring-boot-starter-data-mongodb-reactive, as I want to use the spring data repositories for most of the other queries. Can there be some conflict with that library?

I am not using MongoDB regularly, so I suspect the problem lying with me, but the ChangeStream feature seems to pretty new as well, so I couldn't really find anything on this particular issue on Google.



The following exception is raised (no matter which overloaded version of .watch I try to invoke):
java.lang.IllegalStateException: Invalid Map type. Map must have two type parameters defined.
at org.bson.codecs.pojo.TypeData$Builder.validate(TypeData.java:115) ~[bson-3.5.0.jar:na]
at org.bson.codecs.pojo.TypeData$Builder.build(TypeData.java:104) ~[bson-3.5.0.jar:na]
at org.bson.codecs.pojo.PojoBuilderHelper.getTypeData(PojoBuilderHelper.java:158) ~[bson-3.5.0.jar:na]
at org.bson.codecs.pojo.PojoBuilderHelper.getTypeData(PojoBuilderHelper.java:179) ~[bson-3.5.0.jar:na]
at org.bson.codecs.pojo.PojoBuilderHelper.configureClassModelBuilder(PojoBuilderHelper.java:69) ~[bson-3.5.0.jar:na]
at org.bson.codecs.pojo.ClassModelBuilder.<init>(ClassModelBuilder.java:59) ~[bson-3.5.0.jar:na]
at org.bson.codecs.pojo.ClassModel.builder(ClassModel.java:63) ~[bson-3.5.0.jar:na]
at com.mongodb.client.model.changestream.ChangeStreamDocumentCodec.<init>(ChangeStreamDocumentCodec.java:46) ~[mongodb-driver-core-3.6.0.jar:na]
at com.mongodb.client.model.changestream.ChangeStreamDocument.createCodec(ChangeStreamDocument.java:142) ~[mongodb-driver-core-3.6.0.jar:na]
at com.mongodb.async.client.ChangeStreamIterableImpl.<init>(ChangeStreamIterableImpl.java:62) ~[mongodb-driver-async-3.6.0.jar:na]
at com.mongodb.async.client.MongoCollectionImpl.createChangeStreamIterable(MongoCollectionImpl.java:378) ~[mongodb-driver-async-3.6.0.jar:na]
at com.mongodb.async.client.MongoCollectionImpl.watch(MongoCollectionImpl.java:350) ~[mongodb-driver-async-3.6.0.jar:na]
at com.mongodb.async.client.MongoCollectionImpl.watch(MongoCollectionImpl.java:345) ~[mongodb-driver-async-3.6.0.jar:na]
at com.mypackages.SomeController.test(SomeController.java:63) ~[classes/:na]

Jeff Yemin

unread,
Jan 17, 2018, 6:17:28 PM1/17/18
to mongod...@googlegroups.com
From the stack trace it looks like you're depending on bson-3.5.0.jar.  Please test again using bson-3.6.0.jar instead.


Regards,
Jeff

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/4efbfed3-c486-4b57-b382-97d56dd73afe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff Yemin

unread,
Jan 17, 2018, 6:17:33 PM1/17/18
to mongodb-user
From the stack trace it looks like you're depending on bson-3.5.0.jar.  Please test again using bson-3.6.0.jar.


Regards,
Jeff

Tobi Nonymous

unread,
Jan 17, 2018, 6:35:52 PM1/17/18
to mongodb-user
Good catch, thank you! (must come from somewhere inside the spring deps, as the poms of the mongodb-driver and async-driver reference 3.6.0)

The stacktrace is gone, but I am receiving the following error now:
The $changeStream stage is only supported on replica sets' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "The $changeStream stage is only supported on replica sets", "code" : 40573, "codeName" : "Location40573"

That means if I want to use this feature I have to turn my local mongo installation into a replica set?

Jeff Yemin

unread,
Jan 17, 2018, 7:03:25 PM1/17/18
to mongodb-user
Yes, that's correct.  Change streams work off the oplog, which is present in replica sets but absent in standalones.

For a local mongo installation, you can create a single member replica set.  See this gist for an example.


Regards,
Jeff

Tobi Nonymous

unread,
Jan 17, 2018, 7:09:45 PM1/17/18
to mongodb-user
Great link, thanks a lot for your help!
Reply all
Reply to author
Forward
0 new messages