We’re currently using spring data-mongodb-reactive for change streams API and looking to run couple of admin commands while the springboot application startup. Basically, I want to run a command and execute another one based on the response. How to get the response document from Publisher<Document> commandResult = db.runCommand(bson);
into a variable? here is the whole code snippet.
@Service
public class InitDB {
private final MongoClient mongoClient;
private static final String enableChangeStreams = """
{modifyChangeStreams: 1,
database: "",
collection: "",
enable: true}
""";
private static final String isChangeStreamsEnabled = """
{aggregate: 1,
pipeline: [{$listChangeStreams: 1},
{$match: {$or: [{database: "", collection: ""}]}}
],
cursor:{}}
""";
@Autowired
public InitDB(final MongoClient mongoClient) {
this.mongoClient = mongoClient;
}
public void initializeDB() {
runCommand(isChangeStreamsEnabled);
}
private void runCommand(final String command) {
final MongoDatabase db = mongoClient.getDatabase("admin");
final Document document = Document.parse(command);
final Bson bson = document.toBsonDocument();
final Publisher<Document> commandResult = db.runCommand(bson);
//How to fetch the command result response into a variable via subscribers?
commandResult.subscribe();
final SubscriberHelpers.ObservableSubscriber subscriber = new SubscriberHelpers.ObservableSubscriber();
commandResult.subscribe(subscriber);
System.out.println(subscriber.isCompleted());
}
}