Example usage for com.mongodb.client MongoDatabase runCommand

List of usage examples for com.mongodb.client MongoDatabase runCommand

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase runCommand.

Prototype

Document runCommand(ClientSession clientSession, Bson command);

Source Link

Document

Executes the given command in the context of the current database with a read preference of ReadPreference#primary() .

Usage

From source file:es.omarall.mtc.TailingTask.java

License:Apache License

public TailingTask(MTCConfiguration configuration) {

    // Check configuration is VALID
    configuration.isValid();//from w w w. j a va2  s  .  co m
    LOG.debug("MTCConfiguration: VALID\n{}", configuration.toString());

    this.configuration = configuration;

    MongoDatabase mongoDatabase = configuration.getMongoDatabase();
    String collectionName = configuration.getCollection();
    cappedCollection = mongoDatabase.getCollection(collectionName);

    // Check cappedCollection is a capped collection...
    final Document collStatsCommand = new Document("collStats", collectionName);
    Boolean isCapped = mongoDatabase.runCommand(collStatsCommand, ReadPreference.primary())
            .getBoolean("capped");
    if (!isCapped) {
        throw new CappedCollectionRequiredException(
                "Tailable cursors are only compatible with capped collections, and collection " + collectionName
                        + " is not capped.");
    }

    // Is Capped.
    LOG.debug("Collection {} is CAPPED as expected", collectionName);

    // Persistent TRACKING ENABLED? If enabled tracker != null &&
    // cursorRegenerationDelay != 0
    if (configuration.isPersistentTrackingEnable()) {
        LOG.debug("Persistent tracking is ENABLED");
        tracker = new PersistentTrackingManager(configuration);
        cursorRegenerationDelay = getConfiguration().getPersistentTrackingConfiguration()
                .getCursorRegenerationDelay();
        if (cursorRegenerationDelay == 0) {
            cursorRegenerationDelay = MTCPersistentTrackingConfiguration.DEFAULT_CURSOR_REGENERATION_DELAY;
        }
    }
}

From source file:io.mandrel.common.mongo.MongoUtils.java

License:Apache License

public static void checkCapped(MongoDatabase database, String collectionName, int size, int maxDocuments) {
    if (Lists.newArrayList(database.listCollectionNames()).contains(collectionName)) {
        log.debug("'{}' collection already exists...", collectionName);

        // Check if already capped
        Document command = new Document("collStats", collectionName);
        boolean isCapped = database.runCommand(command, ReadPreference.primary()).getBoolean("capped")
                .booleanValue();//from  ww w. j  a va 2 s  .  c  om

        if (!isCapped) {
            log.info("'{}' is not capped, converting it...", collectionName);
            command = new Document("convertToCapped", collectionName).append("size", size).append("max",
                    maxDocuments);
            database.runCommand(command, ReadPreference.primary());
        } else {
            log.debug("'{}' collection already capped!", collectionName);
        }

    } else {
        database.createCollection(collectionName,
                new CreateCollectionOptions().capped(true).maxDocuments(maxDocuments).sizeInBytes(size));
    }
}