use of com.mongodb.client.MongoDatabase in project presto by prestodb.
the class MongoSession method createTableMetadata.
private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns) throws TableNotFoundException {
String schemaName = schemaTableName.getSchemaName();
String tableName = schemaTableName.getTableName();
MongoDatabase db = client.getDatabase(schemaName);
Document metadata = new Document(TABLE_NAME_KEY, tableName);
ArrayList<Document> fields = new ArrayList<>();
if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) {
fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument());
}
fields.addAll(columns.stream().map(MongoColumnHandle::getDocument).collect(toList()));
metadata.append(FIELDS_KEY, fields);
MongoCollection<Document> schema = db.getCollection(schemaCollection);
schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
schema.insertOne(metadata);
}
use of com.mongodb.client.MongoDatabase in project mongo-java-driver by mongodb.
the class DatabaseAcceptanceTest method shouldBeAbleToListAllTheDatabasesAvailable.
@Test
public void shouldBeAbleToListAllTheDatabasesAvailable() {
MongoClient mongoClient = getMongoClient();
MongoDatabase firstDatabase = mongoClient.getDatabase("FirstNewDatabase");
MongoDatabase secondDatabase = mongoClient.getDatabase("SecondNewDatabase");
MongoDatabase otherDatabase = mongoClient.getDatabase("DatabaseThatDoesNotExistYet");
try {
// given
firstDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
secondDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
//when
List<String> databaseNames = mongoClient.listDatabaseNames().into(new ArrayList<String>());
//then
assertThat(databaseNames, hasItems(firstDatabase.getName(), secondDatabase.getName()));
assertThat(databaseNames, not(hasItem(otherDatabase.getName())));
} finally {
//tear down
firstDatabase.drop();
secondDatabase.drop();
}
}
use of com.mongodb.client.MongoDatabase in project mongo-java-driver by mongodb.
the class ClientAcceptanceTest method shouldBeAbleToListAllTheDatabaseNamesAvailable.
@Test
public void shouldBeAbleToListAllTheDatabaseNamesAvailable() {
MongoDatabase firstDatabase = client.getDatabase("FirstNewDatabase");
MongoDatabase secondDatabase = client.getDatabase("SecondNewDatabase");
MongoDatabase otherDatabase = client.getDatabase("DatabaseThatDoesNotExistYet");
try {
// given
firstDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
secondDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
//when
List<String> databaseNames = client.listDatabaseNames().into(new ArrayList<String>());
//then
assertThat(databaseNames, hasItems(firstDatabase.getName(), secondDatabase.getName()));
assertThat(databaseNames, not(hasItem(otherDatabase.getName())));
} finally {
//tear down
firstDatabase.drop();
secondDatabase.drop();
}
}
use of com.mongodb.client.MongoDatabase in project morphia by mongodb.
the class AggregationTest method testBypassDocumentValidation.
@Test
public void testBypassDocumentValidation() {
checkMinServerVersion(3.2);
getDs().save(asList(new User("john doe", new Date()), new User("John Doe", new Date())));
MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
database.getCollection("out_users").drop();
database.createCollection("out_users", new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(Document.parse("{ age : { gte : 13 } }"))));
try {
getDs().createAggregation(User.class).match(getDs().find(User.class).field("name").equal("john doe")).out("out_users", User.class);
fail("Document validation should have complained.");
} catch (MongoCommandException e) {
// expected
}
getDs().createAggregation(User.class).match(getDs().find(User.class).field("name").equal("john doe")).out("out_users", User.class, builder().bypassDocumentValidation(true).build());
Assert.assertEquals(1, getAds().find("out_users", User.class).count());
}
use of com.mongodb.client.MongoDatabase in project mongo-java-driver by mongodb.
the class Decimal128QuickTour method main.
/**
* Run this main method to see the output of this quick example.
*
* @param args takes an optional single argument for the connection string
*/
public static void main(final String[] args) {
MongoClient mongoClient;
if (args.length == 0) {
// connect to the local database server
mongoClient = new MongoClient();
} else {
mongoClient = new MongoClient(new MongoClientURI(args[0]));
}
// get handle to "mydb" database
MongoDatabase database = mongoClient.getDatabase("mydb");
// get a handle to the "test" collection
MongoCollection<Document> collection = database.getCollection("test");
// drop all the data in it
collection.drop();
// make a document and insert it
Document doc = new Document("name", "MongoDB").append("amount1", Decimal128.parse(".10")).append("amount2", new Decimal128(42L)).append("amount3", new Decimal128(new BigDecimal(".200")));
collection.insertOne(doc);
Document first = collection.find().filter(Filters.eq("amount1", new Decimal128(new BigDecimal(".10")))).first();
Decimal128 amount3 = (Decimal128) first.get("amount3");
BigDecimal amount2AsBigDecimal = amount3.bigDecimalValue();
System.out.println(amount3.toString());
System.out.println(amount2AsBigDecimal.toString());
}
Aggregations