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()));
}
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;
}
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);
}
}
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);
}
}
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);
}
}
Aggregations