import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.changestream.FullDocument;
import com.mongodb.client.model.changestream.ChangeStreamDocument;
import org.bson.Document;
public class StreamChange {
@SuppressWarnings({ "resource" })
public static void main(String[] args) {
Block<ChangeStreamDocument<Document>> printBlock = new Block<ChangeStreamDocument<Document>>() {
@Override
public void apply(final ChangeStreamDocument<Document> changeStreamDocument) {
System.out.println(changeStreamDocument);
}
};
// TODO Auto-generated method stub
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/?w=2"));
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("restaurants");
collection.watch().forEach(printBlock);
}
}
here is the pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MongoDbSteamChange</groupId>
<artifactId>MongoDbSteamChange</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.0-beta2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
behind this code, I use a replicaset, and all the mongod processes has been started with the option --enableMajorityReadConcern
I have also import the test database and the restaurant collection.
First of all, I have to change the following line ( don't know if this is correct, but it seems):
Block<ChangeStreamDocument<Document>> printBlock = new Block<>()
by
Block<ChangeStreamDocument<Document>> printBlock = new Block<ChangeStreamDocument<Document>>()
and now when I make my test, every thing is ok, except when I arrive on the line :
collection.watch().forEach(printBlock);
I have the following message :
Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 72: 'The featureCompatibilityVersion must be 3.6 to use the $changeStream stage. See http://dochub.mongodb.org/core/3.6-feature-compatibility.' on server macbookmax.local:27018. The full response is { "ok" : 0.0, "errmsg" : "The featureCompatibilityVersion must be 3.6 to use the $changeStream stage. See http://dochub.mongodb.org/core/3.6-feature-compatibility.", "code" : 72, "codeName" : "InvalidOptions" }
And as you can guess, I don't know why, and obviously where to search.
Any help would be appreciated
Best regards.
Max
--
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 a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/VV08HCOTlyI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user+unsubscribe@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/050d98e2-33f3-42c5-b43e-0b3455772201%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.