String textUri = "mongodb://admin:password@ds041177.mongolab.com:41177/myclouddb"; MongoClientURI uri = new MongoClientURI(textUri); MongoClient m = new MongoClient(uri);
default MongoClient mongoClient() { return new MongoClient(new MongoClientURI(connectionString())); } }
/** * The MongoDbClient constructor. * @param url The Mongo server url * @param collectionName The Mongo collection to read/write data */ public MongoDbClient(String url, String collectionName) { //Creates a MongoURI from the given string. MongoClientURI uri = new MongoClientURI(url); //Creates a MongoClient described by a URI. this.client = new MongoClient(uri); //Gets a Database. MongoDatabase db = client.getDatabase(uri.getDatabase()); //Gets a collection. this.collection = db.getCollection(collectionName); }
@Signature static public WrapMongoClient createFromURI(Environment env, String uri) { return new WrapMongoClient(env, new MongoClient(new MongoClientURI(uri))); }
MongoClientURI uri = new MongoClientURI( "mongodb://username:password@www.example.com:12345/db-name" ); MongoClient mongoClient = new MongoClient(uri); MongoDatabase db = mongoClient.getDatabase(uri.getDatabase());
protected TestBase() { this(new MongoClient(new MongoClientURI(getMongoURI()))); }
/** * Allows to switch between Fongo and MongoDB based on system parameter {@code mongo}. */ private static MongoClient createClient() { String uri = System.getProperty("mongo"); return uri != null ? new MongoClient(new MongoClientURI(uri)) : new Fongo("FakeMongo").getMongo(); }
protected void configure(final Env env, final Config config, final Binder binder, final BiConsumer<MongoClientURI, MongoClient> callback) { MongoClientOptions.Builder options = options(mongodb(config)); if (this.options != null) { this.options.accept(options, config); } MongoClientURI uri = new MongoClientURI(config.getString(db), options); String database = uri.getDatabase(); checkArgument(database != null, "Database not found: " + uri); MongoClient client = new MongoClient(uri); ServiceKey serviceKey = env.serviceKey(); serviceKey.generate(MongoClientURI.class, database, k -> binder.bind(k).toInstance(uri)); serviceKey.generate(MongoClient.class, database, k -> binder.bind(k).toInstance(client)); MongoDatabase mongodb = client.getDatabase(database); serviceKey.generate(MongoDatabase.class, database, k -> binder.bind(k).toInstance(mongodb)); env.onStop(client::close); callback.accept(uri, client); }
MongoClientURI uri = new MongoClientURI(connectionString);
MongoClientURI uri = new MongoClientURI(url);
public MongoClientURI getMongoClientURI() { final MongoClientOptions.Builder mongoClientOptionsBuilder = MongoClientOptions.builder() .connectionsPerHost(getMaxConnections()) .threadsAllowedToBlockForConnectionMultiplier(getThreadsAllowedToBlockMultiplier()); return new MongoClientURI(uri, mongoClientOptionsBuilder); }
public TestMongoClientWriteConcern() { super(new MongoClient(new MongoClientURI(getMongoURI(), MongoClientOptions.builder().writeConcern(UNACKNOWLEDGED)))); }
mongoClient = new MongoClient(new MongoClientURI(getURI(context))); } else { mongoClient = new MongoClient(new MongoClientURI(getURI(context), getClientOptions(sslContext)));
/** * Creates a MongoURI from a string. * * @param uri the URI * @mongodb.driver.manual reference/connection-string Connection String URI Format * @deprecated Replaced by {@link MongoClientURI#MongoClientURI(String)} */ @SuppressWarnings("deprecation") @Deprecated public MongoURI(final String uri) { this.proxied = new MongoClientURI(uri, MongoClientOptions.builder() .connectionsPerHost(10) .writeConcern(WriteConcern.UNACKNOWLEDGED) ); options = new MongoOptions(proxied.getOptions()); }
mongoClient = new MongoClient(new MongoClientURI(getURI(context))); } else { mongoClient = new MongoClient(new MongoClientURI(getURI(context), getClientOptions(sslContext)));
/** * Create setup using MongoDB client uri. * <ul> * <li>URI should contain database path segment</li> * <li>New internal {@link MongoClient} will be created</li> * <li>New internal executor will be created (with shutdown on jvm exit)</li> * <li>New {@link Gson} instance will be created configured with type adapter factory * providers</li> * </ul> * <p> * Setup created by this factory methods should be reused to configure collection repositories for * the same MongoDB database. * <p> * This constructor designed for ease of use in sample scenarious. For more flexibility consider * using {@link #builder()} with custom constructed {@link ListeningExecutorService * executor} and {@link DB database} handle. * @param uri string that will be parsed as {@link MongoClientURI}. * @see MongoClientURI * @return repository setup instance. */ public static RepositorySetup forUri(String uri) { MongoClientURI clientUri = new MongoClientURI(uri); @Nullable String databaseName = clientUri.getDatabase(); checkArgument(databaseName != null, "URI should contain database path segment"); return builder() .database(newMongoClient(clientUri).getDatabase(databaseName)) .executor(newExecutor()) .gson(createGson()) .build(); }
@Override public ValidationResult validate(String subject, String value, ValidationContext context) { final ValidationResult.Builder builder = new ValidationResult.Builder(); builder.subject(subject).input(value); if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(value)) { return builder.valid(true).explanation("Contains Expression Language").build(); } String reason = null; try { new MongoClientURI(value); } catch (final Exception e) { reason = e.getLocalizedMessage(); } return builder.explanation(reason).valid(reason == null).build(); } };
/** * Create a MongoDB client with params. * * @param hosts list of hosts of the form * "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]" * @return MongoDB client * @see https://docs.mongodb.com/manual/reference/connection-string/ */ public static MongoClient create(String hosts) { MongoClientURI connectionString = new MongoClientURI(hosts); return new MongoClient(connectionString); } }
public MongoDBColl(String url, String db, String coll) { MongoClientURI connectionString = new MongoClientURI(url); mongoClient = new MongoClient(connectionString); MongoDatabase database = mongoClient.getDatabase(db); collection = database.getCollection(coll); }
final MongoClientURI createMongoClientURI(String dataStoreURI) { if (dataStoreURI == null) { throw new IllegalArgumentException("dataStoreURI may not be null"); } if (!dataStoreURI.startsWith("mongodb://")) { throw new IllegalArgumentException( "incorrect scheme for URI, expected to begin with \"mongodb://\", found URI of \"" + dataStoreURI + "\""); } return new MongoClientURI(dataStoreURI.toString()); }