com.mongodb.reactivestreams.client.MongoDatabase Java Examples

The following examples show how to use com.mongodb.reactivestreams.client.MongoDatabase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoClientWrapperTest.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private static void assertWithExpected(final DittoMongoClient mongoClient, final boolean sslEnabled,
        final boolean withCredentials) {

    final MongoClientSettings mongoClientSettings = mongoClient.getSettings();
    assertThat(mongoClientSettings.getClusterSettings().getHosts())
            .isEqualTo(Collections.singletonList(new ServerAddress(KNOWN_SERVER_ADDRESS)));

    final List<MongoCredential> expectedCredentials = withCredentials ? Collections.singletonList(
            MongoCredential.createCredential(KNOWN_USER, KNOWN_DB_NAME, KNOWN_PASSWORD.toCharArray())) :
            Collections.emptyList();
    assertThat(mongoClientSettings.getCredentialList()).isEqualTo(
            expectedCredentials);
    assertThat(mongoClientSettings.getSslSettings().isEnabled()).isEqualTo(sslEnabled);

    final MongoDatabase mongoDatabase = mongoClient.getDefaultDatabase();
    assertThat(mongoDatabase).isNotNull();
    assertThat(mongoDatabase.getName()).isEqualTo(KNOWN_DB_NAME);
}
 
Example #2
Source File: SearchUpdaterRootActor.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private KillSwitch startSearchUpdaterStream(final SearchConfig searchConfig,
        final ActorSystem actorSystem,
        final ShardRegionFactory shardRegionFactory,
        final int numberOfShards,
        final ActorRef updaterShard,
        final ActorRef changeQueueActor,
        final MongoDatabase mongoDatabase,
        final BlockedNamespaces blockedNamespaces) {

    final ActorRef thingsShard = shardRegionFactory.getThingsShardRegion(numberOfShards);
    final ActorRef policiesShard = shardRegionFactory.getPoliciesShardRegion(numberOfShards);

    final SearchUpdaterStream searchUpdaterStream =
            SearchUpdaterStream.of(searchConfig, actorSystem, thingsShard, policiesShard, updaterShard,
                    changeQueueActor, mongoDatabase, blockedNamespaces);

    return searchUpdaterStream.start(getContext());
}
 
Example #3
Source File: DatabaseManager.java    From Shadbot with GNU General Public License v3.0 6 votes vote down vote up
private DatabaseManager() {
    final MongoClientSettings.Builder settingsBuilder = MongoClientSettings.builder()
            .codecRegistry(CODEC_REGISTRY)
            .applicationName(String.format("Shadbot V%s", Config.VERSION));

    if (!Config.IS_SNAPSHOT) {
        final String username = CredentialManager.getInstance().get(Credential.DATABASE_USERNAME);
        final String pwd = CredentialManager.getInstance().get(Credential.DATABASE_PWD);
        final String host = CredentialManager.getInstance().get(Credential.DATABASE_HOST);
        final String port = CredentialManager.getInstance().get(Credential.DATABASE_PORT);
        if (username != null && pwd != null && host != null && port != null) {
            settingsBuilder.applyConnectionString(new ConnectionString(
                    String.format("mongodb://%s:%s@%s:%s/%s", username, pwd, host, port, Config.DATABASE_NAME)));
        }
    }

    this.client = MongoClients.create(settingsBuilder.build());

    final MongoDatabase database = this.client.getDatabase(Config.DATABASE_NAME);
    this.premiumCollection = new PremiumCollection(database);
    this.guildsCollection = new GuildsCollection(database);
    this.lotteryCollection = new LotteryCollection(database);
    this.usersCollection = new UsersCollection(database);
}
 
Example #4
Source File: MongoTimestampPersistence.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private static Source<Success, NotUsed> repeatableCreateCappedCollectionSource(
        final MongoDatabase database,
        final String collectionName,
        final long cappedCollectionSizeInBytes) {

    final CreateCollectionOptions collectionOptions = new CreateCollectionOptions()
            .capped(true)
            .sizeInBytes(cappedCollectionSizeInBytes)
            .maxDocuments(1);

    return Source.lazily(
            () -> Source.fromPublisher(database.createCollection(collectionName, collectionOptions)))
            .mapMaterializedValue(whatever -> NotUsed.getInstance())
            .withAttributes(Attributes.inputBuffer(1, 1))
            .recoverWithRetries(1, new PFBuilder<Throwable, Source<Success, NotUsed>>()
                    .match(MongoCommandException.class,
                            MongoTimestampPersistence::isCollectionAlreadyExistsError,
                            error -> Source.single(Success.SUCCESS))
                    .build());

}
 
Example #5
Source File: MongoTimestampPersistence.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates the capped collection {@code collectionName} using {@code clientWrapper} if it doesn't exists yet.
 *
 * @param database The database to use.
 * @param collectionName The name of the capped collection that should be created.
 * @param cappedCollectionSizeInBytes The size in bytes of the collection that should be created.
 * @param materializer The actor materializer to pre-materialize the restart source.
 * @return Returns the created or retrieved collection.
 */
private static Source<MongoCollection, NotUsed> createOrGetCappedCollection(
        final MongoDatabase database,
        final String collectionName,
        final long cappedCollectionSizeInBytes,
        final ActorMaterializer materializer) {

    final Source<Success, NotUsed> createCollectionSource =
            repeatableCreateCappedCollectionSource(database, collectionName, cappedCollectionSizeInBytes);

    final Source<MongoCollection, NotUsed> infiniteCollectionSource =
            createCollectionSource.map(success -> database.getCollection(collectionName))
                    .flatMapConcat(Source::repeat);

    final Source<MongoCollection, NotUsed> restartSource =
            RestartSource.withBackoff(BACKOFF_MIN, BACKOFF_MAX, 1.0, () -> infiniteCollectionSource);

    // pre-materialize source with BroadcastHub so that a successfully obtained capped collection is reused
    // until the stream fails, whereupon it gets recreated with backoff.
    return restartSource.runWith(BroadcastHub.of(MongoCollection.class, 1), materializer);
}
 
Example #6
Source File: MongoSink.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Map<String, Object> config, SinkContext sinkContext) throws Exception {
    log.info("Open MongoDB Sink");

    mongoConfig = MongoConfig.load(config);
    mongoConfig.validate(true, true);

    if (clientProvider != null) {
        mongoClient = clientProvider.get();
    } else {
        mongoClient = MongoClients.create(mongoConfig.getMongoUri());
    }

    final MongoDatabase db = mongoClient.getDatabase(mongoConfig.getDatabase());
    collection = db.getCollection(mongoConfig.getCollection());

    incomingList = Lists.newArrayList();
    flushExecutor = Executors.newScheduledThreadPool(1);
    flushExecutor.scheduleAtFixedRate(() -> flush(),
            mongoConfig.getBatchTimeMs(), mongoConfig.getBatchTimeMs(), TimeUnit.MILLISECONDS);
}
 
Example #7
Source File: PolicyPersistenceOperationsActor.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Create Props of this actor.
 *
 * @param pubSubMediator Akka pub-sub mediator.
 * @param mongoDbConfig the MongoDB configuration settings.
 * @param config Configuration with info about event journal, snapshot store and database.
 * @param persistenceOperationsConfig the persistence operations configuration settings.
 * @return a Props object.
 */
public static Props props(final ActorRef pubSubMediator,
        final MongoDbConfig mongoDbConfig,
        final Config config,
        final PersistenceOperationsConfig persistenceOperationsConfig) {

    return Props.create(PolicyPersistenceOperationsActor.class, () -> {
        final MongoEventSourceSettings eventSourceSettings =
                MongoEventSourceSettings.fromConfig(config, PolicyPersistenceActor.PERSISTENCE_ID_PREFIX, true,
                        PolicyPersistenceActor.JOURNAL_PLUGIN_ID, PolicyPersistenceActor.SNAPSHOT_PLUGIN_ID);

        final MongoClientWrapper mongoClient = MongoClientWrapper.newInstance(mongoDbConfig);
        final MongoDatabase db = mongoClient.getDefaultDatabase();

        final NamespacePersistenceOperations namespaceOps =
                MongoNamespacePersistenceOperations.of(db, eventSourceSettings);
        final EntityPersistenceOperations entitiesOps =
                MongoEntitiesPersistenceOperations.of(db, eventSourceSettings);

        return new PolicyPersistenceOperationsActor(pubSubMediator, namespaceOps, entitiesOps, mongoClient,
                persistenceOperationsConfig);
    });
}
 
Example #8
Source File: ConnectionPersistenceOperationsActor.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Create Props of this actor.
 *
 * @param pubSubMediator Akka pub-sub mediator.
 * @param mongoDbConfig the MongoDB configuration settings.
 * @param config configuration with info about event journal, snapshot store and database.
 * @param persistenceOperationsConfig the persistence operations configuration settings.
 * @return a Props object.
 */
public static Props props(final ActorRef pubSubMediator,
        final MongoDbConfig mongoDbConfig,
        final Config config,
        final PersistenceOperationsConfig persistenceOperationsConfig) {

    return Props.create(ConnectionPersistenceOperationsActor.class, () -> {
        final MongoEventSourceSettings eventSourceSettings =
                MongoEventSourceSettings.fromConfig(config, ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX, false,
                        ConnectionPersistenceActor.JOURNAL_PLUGIN_ID, ConnectionPersistenceActor.SNAPSHOT_PLUGIN_ID);

        final MongoClientWrapper mongoClient = MongoClientWrapper.newInstance(mongoDbConfig);
        final MongoDatabase db = mongoClient.getDefaultDatabase();

        final EntityPersistenceOperations entitiesOps =
                MongoEntitiesPersistenceOperations.of(db, eventSourceSettings);

        return new ConnectionPersistenceOperationsActor(pubSubMediator, entitiesOps, mongoClient,
                persistenceOperationsConfig);
    });
}
 
Example #9
Source File: MongoSinkTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setUp() {

    map = TestHelper.createMap(true);

    mockRecord = mock(Record.class);
    mockSinkContext = mock(SinkContext.class);
    mockMongoClient = mock(MongoClient.class);
    mockMongoDb = mock(MongoDatabase.class);
    mockMongoColl = mock(MongoCollection.class);
    mockPublisher = mock(Publisher.class);
    sink = new MongoSink(() -> mockMongoClient);


    when(mockMongoClient.getDatabase(anyString())).thenReturn(mockMongoDb);
    when(mockMongoDb.getCollection(anyString())).thenReturn(mockMongoColl);
    when(mockMongoDb.getCollection(anyString()).insertMany(any())).thenReturn(mockPublisher);
}
 
Example #10
Source File: ThingPersistenceOperationsActor.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Create Props of this actor.
 *
 * @param pubSubMediator Akka pub-sub mediator.
 * @param mongoDbConfig the MongoDB configuration settings.
 * @param config Configuration with info about event journal, snapshot store and database.
 * @param persistenceOperationsConfig the persistence operations config.
 * @return a Props object.
 */
public static Props props(final ActorRef pubSubMediator,
        final MongoDbConfig mongoDbConfig,
        final Config config,
        final PersistenceOperationsConfig persistenceOperationsConfig) {

    return Props.create(ThingPersistenceOperationsActor.class, () -> {
        final MongoEventSourceSettings eventSourceSettings =
                MongoEventSourceSettings.fromConfig(config, ThingPersistenceActor.PERSISTENCE_ID_PREFIX, true,
                        ThingPersistenceActor.JOURNAL_PLUGIN_ID, ThingPersistenceActor.SNAPSHOT_PLUGIN_ID);

        final MongoClientWrapper mongoClient = MongoClientWrapper.newInstance(mongoDbConfig);
        final MongoDatabase db = mongoClient.getDefaultDatabase();

        final NamespacePersistenceOperations namespaceOps =
                MongoNamespacePersistenceOperations.of(db, eventSourceSettings);

        return new ThingPersistenceOperationsActor(pubSubMediator, namespaceOps, mongoClient,
                persistenceOperationsConfig);
    });
}
 
Example #11
Source File: MongoSourceTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setUp() {

    map = TestHelper.createMap(true);

    mockSourceContext = mock(SourceContext.class);
    mockMongoClient = mock(MongoClient.class);
    mockMongoDb = mock(MongoDatabase.class);
    mockMongoColl = mock(MongoCollection.class);
    mockPublisher = mock(ChangeStreamPublisher.class);

    source = new MongoSource(() -> mockMongoClient);

    when(mockMongoClient.getDatabase(anyString())).thenReturn(mockMongoDb);
    when(mockMongoDb.getCollection(anyString())).thenReturn(mockMongoColl);
    when(mockMongoColl.watch()).thenReturn(mockPublisher);
    when(mockPublisher.batchSize(anyInt())).thenReturn(mockPublisher);
    when(mockPublisher.fullDocument(any())).thenReturn(mockPublisher);

    doAnswer((invocation) -> {
        subscriber = invocation.getArgument(0, Subscriber.class);
        return null;
    }).when(mockPublisher).subscribe(any());
}
 
Example #12
Source File: CollectionResolver.java    From immutables with Apache License 2.0 5 votes vote down vote up
static CollectionResolver defaultResolver(MongoDatabase database, CodecRegistry registry) {
  Objects.requireNonNull(database, "database");
  Objects.requireNonNull(registry, "registry");
  return entityClass -> {
    final String collectionName = ContainerNaming.DEFAULT.name(entityClass);
    return database.getCollection(collectionName)
            .withDocumentClass(entityClass)
            .withCodecRegistry(registry);
  };
}
 
Example #13
Source File: GridFSBuckets.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new GridFS bucket with a custom bucket name
 *
 * <p>Requires the concrete {@link MongoDatabaseImpl} implementation of the MongoDatabase interface.</p>
 *
 * @param database   the database instance to use with GridFS
 * @param bucketName the custom bucket name to use
 * @return the GridFSBucket
 */
public static GridFSBucket create(final MongoDatabase database, final String bucketName) {
    notNull("database", database);
    notNull("bucketName", bucketName);
    if (database instanceof MongoDatabaseImpl) {
        return new GridFSBucketImpl(com.mongodb.async.client.gridfs.GridFSBuckets.create(((MongoDatabaseImpl) database).getWrapped(),
                bucketName));
    } else {
        throw new IllegalArgumentException("GridFS requires the concrete MongoDatabaseImpl implementation.");
    }
}
 
Example #14
Source File: MongoReadJournal.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private Source<Document, NotUsed> find(final MongoDatabase db, final String collection, final Document filter,
        final Document project) {

    return Source.fromPublisher(
            db.getCollection(collection).find(filter).projection(project).sort(ID_DESC)
    );
}
 
Example #15
Source File: GridFSBuckets.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new GridFS bucket with the default {@code 'fs'} bucket name
 *
 * <p>Requires the concrete {@link MongoDatabaseImpl} implementation of the MongoDatabase interface.</p>
 *
 * @param database the database instance to use with GridFS.
 * @return the GridFSBucket
 */
public static GridFSBucket create(final MongoDatabase database) {
    notNull("database", database);
    if (database instanceof MongoDatabaseImpl) {
        return new GridFSBucketImpl(com.mongodb.async.client.gridfs.GridFSBuckets.create(((MongoDatabaseImpl) database).getWrapped()));
    } else {
        throw new IllegalArgumentException("GridFS requires the concrete MongoDatabaseImpl implementation.");
    }
}
 
Example #16
Source File: SearchUpdaterStream.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Create a restart-able SearchUpdaterStream object.
 *
 * @param searchConfig the configuration settings of the Things-Search service.
 * @param actorSystem actor system to run the stream in.
 * @param thingsShard shard region proxy of things.
 * @param policiesShard shard region proxy of policies.
 * @param updaterShard shard region of search updaters.
 * @param changeQueueActor reference of the change queue actor.
 * @param database MongoDB database.
 * @return a SearchUpdaterStream object.
 */
public static SearchUpdaterStream of(final SearchConfig searchConfig,
        final ActorSystem actorSystem,
        final ActorRef thingsShard,
        final ActorRef policiesShard,
        final ActorRef updaterShard,
        final ActorRef changeQueueActor,
        final MongoDatabase database,
        final BlockedNamespaces blockedNamespaces) {

    final StreamConfig streamConfig = searchConfig.getStreamConfig();

    final StreamCacheConfig cacheConfig = streamConfig.getCacheConfig();
    final String dispatcherName = cacheConfig.getDispatcherName();
    final MessageDispatcher messageDispatcher = actorSystem.dispatchers().lookup(dispatcherName);

    final DeleteConfig deleteConfig = searchConfig.getDeleteConfig();
    final boolean deleteEvent = deleteConfig.isDeleteEvent();

    final EnforcementFlow enforcementFlow =
            EnforcementFlow.of(streamConfig, thingsShard, policiesShard, messageDispatcher,
                    deleteEvent);

    final MongoSearchUpdaterFlow mongoSearchUpdaterFlow = MongoSearchUpdaterFlow.of(database);

    final BulkWriteResultAckFlow bulkWriteResultAckFlow = BulkWriteResultAckFlow.of(updaterShard);

    return new SearchUpdaterStream(searchConfig, enforcementFlow, mongoSearchUpdaterFlow, bulkWriteResultAckFlow,
            changeQueueActor, blockedNamespaces);
}
 
Example #17
Source File: MongoThingsSearchPersistence.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Initializes the things search persistence with a passed in {@code persistence}.
 *
 * @param mongoClient the mongoDB persistence wrapper.
 * @param actorSystem the Akka ActorSystem.
 * @since 1.0.0
 */
public MongoThingsSearchPersistence(final DittoMongoClient mongoClient, final ActorSystem actorSystem) {
    final MongoDatabase database = mongoClient.getDefaultDatabase();
    // configure search persistence to stress the primary as little as possible and tolerate inconsistency
    collection = database
            .getCollection(PersistenceConstants.THINGS_COLLECTION_NAME)
            .withReadPreference(ReadPreference.secondaryPreferred());

    log = Logging.getLogger(actorSystem, getClass());
    final ActorMaterializer materializer = ActorMaterializer.create(actorSystem);
    indexInitializer = IndexInitializer.of(database, materializer);
    maxQueryTime = mongoClient.getDittoSettings().getMaxQueryTime();
    hints = MongoHints.empty();
}
 
Example #18
Source File: BackendResource.java    From immutables with Apache License 2.0 5 votes vote down vote up
BackendResource(MongoDatabase database) {
  this.database = Objects.requireNonNull(database, "database");
  final ObjectMapper mapper = new ObjectMapper()
          .registerModule(new BsonModule())
          .registerModule(new GuavaModule())
          .registerModule(new Jdk8Module())
          .registerModule(new IdAnnotationModule());

  this.registry = JacksonCodecs.registryFromMapper(mapper);
  this.resolver = new LazyResolver();
  this.backend = new MongoBackend(MongoSetup.of(this.resolver));
}
 
Example #19
Source File: MongoExtension.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
  final Class<?> type = parameterContext.getParameter().getType();
  if (MongoDatabase.class.isAssignableFrom(type)) {
    return getOrCreate(extensionContext).instance.database();
  } else if (MongoClient.class.isAssignableFrom(type)) {
    return getOrCreate(extensionContext).instance.client();
  }

  throw new ExtensionConfigurationException(String.format("%s supports only %s or %s but yours was %s", MongoExtension.class.getSimpleName(),
          MongoDatabase.class.getName(), MongoClient.class.getName(), type.getName()));
}
 
Example #20
Source File: MongoExtension.java    From immutables with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("CheckReturnValue")
private void clear() {
  // drop all collections
  MongoDatabase database = instance.database();
  Flowable.fromPublisher(database.listCollectionNames())
          .flatMap(col -> database.getCollection(col).drop())
          .toList()
          .blockingGet();
}
 
Example #21
Source File: MongoPersonTest.java    From immutables with Apache License 2.0 4 votes vote down vote up
MongoPersonTest(MongoDatabase database) {
  this.backend = new BackendResource(database).backend();
}
 
Example #22
Source File: MongoIntegrationTest.java    From immutables with Apache License 2.0 4 votes vote down vote up
MongoIntegrationTest(MongoDatabase database) {
  this.backend = new BackendResource(database).backend();
}
 
Example #23
Source File: MongoAggregationTest.java    From immutables with Apache License 2.0 4 votes vote down vote up
MongoAggregationTest(MongoDatabase database) {
  this.backend = new BackendResource(database);
}
 
Example #24
Source File: BackendResource.java    From immutables with Apache License 2.0 4 votes vote down vote up
MongoDatabase database() {
  return database;
}
 
Example #25
Source File: OAuth2RepositoryTestConfiguration.java    From graviteeio-access-management with Apache License 2.0 4 votes vote down vote up
@Bean(name = "oauth2MongoTemplate")
public MongoDatabase mongoOperations() {
    return embeddedClient().mongoDatabase();
}
 
Example #26
Source File: QuickTourAdmin.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 * @throws Throwable if an operation fails
 */
public static void main(final String[] args) throws Throwable {
    MongoClient mongoClient;

    if (args.length == 0) {
        // connect to the local database server
        mongoClient = MongoClients.create();
    } else {
        mongoClient = MongoClients.create(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");
    ObservableSubscriber subscriber = new ObservableSubscriber<Success>();
    collection.drop().subscribe(subscriber);
    subscriber.await();

    // getting a list of databases
    mongoClient.listDatabaseNames().subscribe(new PrintSubscriber<String>("Database Names: %s"));

    // drop a database
    subscriber = new ObservableSubscriber<Success>();
    mongoClient.getDatabase("databaseToBeDropped").drop().subscribe(subscriber);
    subscriber.await();

    // create a collection
    database.createCollection("cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000))
                .subscribe(new PrintSubscriber<Success>("Creation Created!"));


    database.listCollectionNames().subscribe(new PrintSubscriber<String>("Collection Names: %s"));

    // drop a collection:
    subscriber = new ObservableSubscriber<Success>();
    collection.drop().subscribe(subscriber);
    subscriber.await();

    // create an ascending index on the "i" field
    collection.createIndex(new Document("i", 1)).subscribe(new PrintSubscriber<String>("Created an index named: %s"));

    // list the indexes on the collection
    collection.listIndexes().subscribe(new PrintDocumentSubscriber());


    // create a text index on the "content" field
    subscriber = new PrintSubscriber<String>("Created an index named: %s");
    collection.createIndex(new Document("content", "text")).subscribe(subscriber);
    subscriber.await();

    subscriber = new OperationSubscriber();
    collection.insertMany(asList(new Document("_id", 0).append("content", "textual content"),
            new Document("_id", 1).append("content", "additional content"),
            new Document("_id", 2).append("content", "irrelevant content"))).subscribe(subscriber);
    subscriber.await();

    // Find using the text index
    subscriber = new PrintSubscriber("Text search matches: %s");
    collection.countDocuments(text("textual content -irrelevant")).subscribe(subscriber);
    subscriber.await();

    // Find using the $language operator
    subscriber = new PrintSubscriber("Text search matches (english): %s");
    Bson textSearch = text("textual content -irrelevant", new TextSearchOptions().language("english"));
    collection.countDocuments(textSearch).subscribe(subscriber);
    subscriber.await();

    // Find the highest scoring match
    System.out.print("Highest scoring document: ");
    Document projection = new Document("score", new Document("$meta", "textScore"));
    collection.find(textSearch).projection(projection).first().subscribe(new PrintDocumentSubscriber());


    // Run a command
    database.runCommand(new Document("buildInfo", 1)).subscribe(new PrintDocumentSubscriber());

    // release resources
    subscriber = new OperationSubscriber();
    database.drop().subscribe(subscriber);
    subscriber.await();
    mongoClient.close();
}
 
Example #27
Source File: ReactiveStreamsMongoLockProvider.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
/**
 * Uses Mongo to coordinate locks
 */
public ReactiveStreamsMongoLockProvider(@NonNull MongoDatabase mongoDatabase) {
    this(mongoDatabase.getCollection(DEFAULT_SHEDLOCK_COLLECTION_NAME));
}
 
Example #28
Source File: EmbeddedClient.java    From graviteeio-access-management with Apache License 2.0 4 votes vote down vote up
public MongoDatabase mongoDatabase() {
    return mongoDatabase;
}
 
Example #29
Source File: MongoAuthenticationProviderTestConfiguration.java    From graviteeio-access-management with Apache License 2.0 4 votes vote down vote up
@Bean
public MongoDatabase mongoDatabase() {
    return embeddedClient().mongoDatabase();
}
 
Example #30
Source File: MongoInstance.java    From immutables with Apache License 2.0 4 votes vote down vote up
MongoDatabase database() {
  return database;
}