Search in sources :

Example 6 with MongoCommandException

use of com.mongodb.MongoCommandException in project morphia by mongodb.

the class TestMapreduce method testBypassDocumentValidation.

@Test
public void testBypassDocumentValidation() {
    checkMinServerVersion(3.4);
    getDs().save(asList(new Book("The Banquet", "Dante", 2), new Book("Divine Comedy", "Dante", 1), new Book("Eclogues", "Dante", 2), new Book("The Odyssey", "Homer", 10), new Book("Iliad", "Homer", 10)));
    Document validator = Document.parse("{ count : { $gt : '10' } }");
    ValidationOptions validationOptions = new ValidationOptions().validator(validator).validationLevel(ValidationLevel.STRICT).validationAction(ValidationAction.ERROR);
    MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
    database.getCollection("counts").drop();
    database.createCollection("counts", new CreateCollectionOptions().validationOptions(validationOptions));
    final String map = "function () { emit(this.author, 1); return; }";
    final String reduce = "function (key, values) { return values.length }";
    MapReduceOptions<CountResult> options = new MapReduceOptions<CountResult>().query(getDs().find(Book.class)).resultType(CountResult.class).outputType(OutputType.REPLACE).map(map).reduce(reduce);
    try {
        getDs().mapReduce(options);
        fail("Document validation should have complained.");
    } catch (MongoCommandException e) {
    // expected
    }
    getDs().mapReduce(options.bypassDocumentValidation(true));
    Assert.assertEquals(2, count(getDs().find(CountResult.class).iterator()));
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) Book(org.mongodb.morphia.aggregation.AggregationTest.Book) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) ValidationOptions(com.mongodb.client.model.ValidationOptions) CountResult(org.mongodb.morphia.aggregation.AggregationTest.CountResult) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 7 with MongoCommandException

use of com.mongodb.MongoCommandException in project graylog2-server by Graylog2.

the class MongoConnectionImpl method connect.

/**
     * Connect the instance.
     */
@Override
public synchronized Mongo connect() {
    if (m == null) {
        final String dbName = mongoClientURI.getDatabase();
        if (isNullOrEmpty(dbName)) {
            LOG.error("The MongoDB database name must not be null or empty (mongodb_uri was: {})", mongoClientURI);
            throw new RuntimeException("MongoDB database name is missing.");
        }
        m = new MongoClient(mongoClientURI);
        db = m.getDB(dbName);
        db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        mongoDatabase = m.getDatabase(dbName).withWriteConcern(WriteConcern.ACKNOWLEDGED);
    }
    try {
        db.command("{ ping: 1 }");
    } catch (MongoCommandException e) {
        if (e.getCode() == 18) {
            throw new MongoException("Couldn't connect to MongoDB. Please check the authentication credentials.", e);
        } else {
            throw new MongoException("Couldn't connect to MongoDB: " + e.getMessage(), e);
        }
    }
    final Version mongoVersion = getMongoVersion(m.getDB("admin"));
    if (mongoVersion != null && mongoVersion.lessThan(MINIMUM_MONGODB_VERSION)) {
        LOG.warn("You're running MongoDB {} but Graylog requires at least MongoDB {}. Please upgrade.", mongoVersion, MINIMUM_MONGODB_VERSION);
    }
    return m;
}
Also used : MongoClient(com.mongodb.MongoClient) MongoException(com.mongodb.MongoException) MongoCommandException(com.mongodb.MongoCommandException) Version(com.github.zafarkhaja.semver.Version)

Example 8 with MongoCommandException

use of com.mongodb.MongoCommandException in project mongo-java-driver by mongodb.

the class X509Authenticator method authenticate.

@Override
void authenticate(final InternalConnection connection, final ConnectionDescription connectionDescription) {
    try {
        validateUserName(connectionDescription);
        BsonDocument authCommand = getAuthCommand(getCredential().getUserName());
        executeCommand(getCredential().getSource(), authCommand, connection);
    } catch (MongoCommandException e) {
        throw new MongoSecurityException(getCredential(), "Exception authenticating", e);
    }
}
Also used : MongoSecurityException(com.mongodb.MongoSecurityException) BsonDocument(org.bson.BsonDocument) MongoCommandException(com.mongodb.MongoCommandException)

Example 9 with MongoCommandException

use of com.mongodb.MongoCommandException in project mongo-java-driver by mongodb.

the class NativeAuthenticator method authenticate.

@Override
public void authenticate(final InternalConnection connection, final ConnectionDescription connectionDescription) {
    try {
        BsonDocument nonceResponse = executeCommand(getCredential().getSource(), getNonceCommand(), connection);
        BsonDocument authCommand = getAuthCommand(getCredential().getUserName(), getCredential().getPassword(), ((BsonString) nonceResponse.get("nonce")).getValue());
        executeCommand(getCredential().getSource(), authCommand, connection);
    } catch (MongoCommandException e) {
        throw new MongoSecurityException(getCredential(), "Exception authenticating", e);
    }
}
Also used : MongoSecurityException(com.mongodb.MongoSecurityException) BsonDocument(org.bson.BsonDocument) MongoCommandException(com.mongodb.MongoCommandException)

Example 10 with MongoCommandException

use of com.mongodb.MongoCommandException in project mongo-java-driver by mongodb.

the class Fixture method drop.

public static void drop(final MongoNamespace namespace) {
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        getMongoClient().getDatabase(namespace.getDatabaseName()).runCommand(new Document("drop", namespace.getCollectionName()), futureResultCallback);
        futureResultCallback.get();
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().contains("ns not found")) {
            throw e;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoCommandException(com.mongodb.MongoCommandException) Document(org.bson.Document)

Aggregations

MongoCommandException (com.mongodb.MongoCommandException)11 Document (org.bson.Document)4 FutureResultCallback (com.mongodb.async.FutureResultCallback)3 BsonDocument (org.bson.BsonDocument)3 Test (org.junit.Test)3 MongoSecurityException (com.mongodb.MongoSecurityException)2 MongoDatabase (com.mongodb.client.MongoDatabase)2 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)2 ValidationOptions (com.mongodb.client.model.ValidationOptions)2 Date (java.util.Date)2 Version (com.github.zafarkhaja.semver.Version)1 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1 MongoQueryException (com.mongodb.MongoQueryException)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 Book (org.mongodb.morphia.aggregation.AggregationTest.Book)1 CountResult (org.mongodb.morphia.aggregation.AggregationTest.CountResult)1 DocumentValidation (org.mongodb.morphia.entities.DocumentValidation)1