use of org.bson.Document in project morphia by mongodb.
the class TestDocumentValidation method overwriteValidation.
@Test
public void overwriteValidation() {
Document validator = Document.parse("{ jelly : { $ne : 'rhubarb' } }");
MongoDatabase database = addValidation(validator, "validation");
assertEquals(validator, getValidator());
Document rhubarb = new Document("jelly", "rhubarb").append("number", 20);
database.getCollection("validation").insertOne(new Document("jelly", "grape"));
try {
database.getCollection("validation").insertOne(rhubarb);
fail("Document should have failed validation");
} catch (MongoWriteException e) {
assertTrue(e.getMessage().contains("Document failed validation"));
}
getMorphia().map(DocumentValidation.class);
getDs().enableDocumentValidation();
assertEquals(Document.parse(DocumentValidation.class.getAnnotation(Validation.class).value()), getValidator());
try {
database.getCollection("validation").insertOne(rhubarb);
} catch (MongoWriteException e) {
assertFalse(e.getMessage().contains("Document failed validation"));
}
try {
getDs().save(new DocumentValidation("John", 1, new Date()));
fail("Document should have failed validation");
} catch (WriteConcernException e) {
assertTrue(e.getMessage().contains("Document failed validation"));
}
}
use of org.bson.Document in project morphia by mongodb.
the class TestDocumentValidation method saveToNewCollection.
@Test
public void saveToNewCollection() {
getMorphia().map(DocumentValidation.class);
final Document validator = Document.parse("{ number : { $gt : 10 } }");
String collection = "newdocs";
addValidation(validator, collection);
try {
getAds().save(collection, new DocumentValidation("Harold", 8, new Date()));
fail("Document validation should have complained");
} catch (WriteConcernException e) {
// expected
}
getAds().save(collection, new DocumentValidation("Harold", 8, new Date()), new InsertOptions().bypassDocumentValidation(true));
Query<DocumentValidation> query = getAds().createQuery(collection, DocumentValidation.class).field("number").equal(8);
Assert.assertNotNull(query.get());
}
use of org.bson.Document 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 org.bson.Document in project presto by prestodb.
the class MongoSession method guessFieldType.
private Optional<TypeSignature> guessFieldType(Object value) {
if (value == null) {
return Optional.empty();
}
TypeSignature typeSignature = null;
if (value instanceof String) {
typeSignature = createUnboundedVarcharType().getTypeSignature();
} else if (value instanceof Integer || value instanceof Long) {
typeSignature = BIGINT.getTypeSignature();
} else if (value instanceof Boolean) {
typeSignature = BOOLEAN.getTypeSignature();
} else if (value instanceof Float || value instanceof Double) {
typeSignature = DOUBLE.getTypeSignature();
} else if (value instanceof Date) {
typeSignature = TIMESTAMP.getTypeSignature();
} else if (value instanceof ObjectId) {
typeSignature = OBJECT_ID.getTypeSignature();
} else if (value instanceof List) {
List<Optional<TypeSignature>> subTypes = ((List<?>) value).stream().map(this::guessFieldType).collect(toList());
if (subTypes.isEmpty() || subTypes.stream().anyMatch(t -> !t.isPresent())) {
return Optional.empty();
}
Set<TypeSignature> signatures = subTypes.stream().map(t -> t.get()).collect(toSet());
if (signatures.size() == 1) {
typeSignature = new TypeSignature(StandardTypes.ARRAY, signatures.stream().map(s -> TypeSignatureParameter.of(s)).collect(Collectors.toList()));
} else {
// TODO: presto cli doesn't handle empty field name row type yet
typeSignature = new TypeSignature(StandardTypes.ROW, IntStream.range(0, subTypes.size()).mapToObj(idx -> TypeSignatureParameter.of(new NamedTypeSignature(String.format("%s%d", implicitPrefix, idx + 1), subTypes.get(idx).get()))).collect(toList()));
}
} else if (value instanceof Document) {
List<TypeSignatureParameter> parameters = new ArrayList<>();
for (String key : ((Document) value).keySet()) {
Optional<TypeSignature> fieldType = guessFieldType(((Document) value).get(key));
if (!fieldType.isPresent()) {
return Optional.empty();
}
parameters.add(TypeSignatureParameter.of(new NamedTypeSignature(key, fieldType.get())));
}
typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
}
return Optional.ofNullable(typeSignature);
}
use of org.bson.Document in project presto by prestodb.
the class MongoSession method execute.
public MongoCursor<Document> execute(MongoSplit split, List<MongoColumnHandle> columns) {
Document output = new Document();
for (MongoColumnHandle column : columns) {
output.append(column.getName(), 1);
}
MongoCollection<Document> collection = getCollection(split.getSchemaTableName());
FindIterable<Document> iterable = collection.find(buildQuery(split.getTupleDomain())).projection(output);
if (cursorBatchSize != 0) {
iterable.batchSize(cursorBatchSize);
}
return iterable.iterator();
}
Aggregations