Class MongoUtil

java.lang.Object
io.debezium.connector.mongodb.MongoUtil

public class MongoUtil extends Object
Utilities for working with MongoDB.
Author:
Randall Hauch
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.mongodb.connection.ClusterDescription
    clusterDescription(com.mongodb.client.MongoClient client)
    Retrieves cluster description, forcing a connection if not yet available
    static <T> boolean
    contains(com.mongodb.client.MongoIterable<String> iterable, String match)
    Determine if the supplied MongoIterable contains an element that is equal to the supplied value.
    static <T> boolean
    contains(com.mongodb.client.MongoIterable<T> iterable, Predicate<T> matcher)
    Determine if the supplied MongoIterable contains at least one element that satisfies the given predicate.
    static <T> void
    forEach(com.mongodb.client.MongoIterable<T> iterable, Consumer<T> operation)
    Perform the given operation on each of the values in the iterable container.
    static void
    forEachCollectionNameInDatabase(com.mongodb.client.MongoClient client, String databaseName, Consumer<String> operation)
    Perform the given operation on each of the collection names in the named database.
    static void
    forEachDatabaseName(com.mongodb.client.MongoClient client, Consumer<String> operation)
    Perform the given operation on each of the database names.
    getChangeStreamSessionTransactionId(com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.BsonDocument> event)
    Helper function to extract the session transaction-id from an Change Stream event.
    static void
    onCollection(com.mongodb.client.MongoClient client, String dbName, String collectionName, Consumer<com.mongodb.client.MongoCollection<org.bson.Document>> collectionOperation)
    Perform the given operation on the named collection in the named database, if the database and collection both exist.
    static void
    onCollectionDocuments(com.mongodb.client.MongoClient client, String dbName, String collectionName, BlockingConsumer<org.bson.Document> documentOperation)
    Perform the given operation on all of the documents inside the named collection in the named database, if the database and collection both exist.
    static void
    onDatabase(com.mongodb.client.MongoClient client, String dbName, Consumer<com.mongodb.client.MongoDatabase> dbOperation)
    Perform the given operation on the database with the given name, only if that database exists.
    replicaSetName(com.mongodb.connection.ClusterDescription clusterDescription)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MongoUtil

      private MongoUtil()
  • Method Details

    • forEachDatabaseName

      public static void forEachDatabaseName(com.mongodb.client.MongoClient client, Consumer<String> operation)
      Perform the given operation on each of the database names.
      Parameters:
      client - the MongoDB client; may not be null
      operation - the operation to perform; may not be null
    • forEachCollectionNameInDatabase

      public static void forEachCollectionNameInDatabase(com.mongodb.client.MongoClient client, String databaseName, Consumer<String> operation)
      Perform the given operation on each of the collection names in the named database.
      Parameters:
      client - the MongoDB client; may not be null
      databaseName - the name of the database; may not be null
      operation - the operation to perform; may not be null
    • forEach

      public static <T> void forEach(com.mongodb.client.MongoIterable<T> iterable, Consumer<T> operation)
      Perform the given operation on each of the values in the iterable container.
      Parameters:
      iterable - the iterable collection obtained from a MongoDB client; may not be null
      operation - the operation to perform; may not be null
    • onDatabase

      public static void onDatabase(com.mongodb.client.MongoClient client, String dbName, Consumer<com.mongodb.client.MongoDatabase> dbOperation)
      Perform the given operation on the database with the given name, only if that database exists.
      Parameters:
      client - the MongoDB client; may not be null
      dbName - the name of the database; may not be null
      dbOperation - the operation to perform; may not be null
    • onCollection

      public static void onCollection(com.mongodb.client.MongoClient client, String dbName, String collectionName, Consumer<com.mongodb.client.MongoCollection<org.bson.Document>> collectionOperation)
      Perform the given operation on the named collection in the named database, if the database and collection both exist.
      Parameters:
      client - the MongoDB client; may not be null
      dbName - the name of the database; may not be null
      collectionName - the name of the collection; may not be null
      collectionOperation - the operation to perform; may not be null
    • onCollectionDocuments

      public static void onCollectionDocuments(com.mongodb.client.MongoClient client, String dbName, String collectionName, BlockingConsumer<org.bson.Document> documentOperation)
      Perform the given operation on all of the documents inside the named collection in the named database, if the database and collection both exist. The operation is called once for each document, so if the collection exists but is empty then the function will not be called.
      Parameters:
      client - the MongoDB client; may not be null
      dbName - the name of the database; may not be null
      collectionName - the name of the collection; may not be null
      documentOperation - the operation to perform; may not be null
    • contains

      public static <T> boolean contains(com.mongodb.client.MongoIterable<String> iterable, String match)
      Determine if the supplied MongoIterable contains an element that is equal to the supplied value.
      Parameters:
      iterable - the iterable; may not be null
      match - the value to find in the iterable; may be null
      Returns:
      true if a matching value was found, or false otherwise
    • contains

      public static <T> boolean contains(com.mongodb.client.MongoIterable<T> iterable, Predicate<T> matcher)
      Determine if the supplied MongoIterable contains at least one element that satisfies the given predicate.
      Parameters:
      iterable - the iterable; may not be null
      matcher - the predicate function called on each value in the iterable until a match is found; may not be null
      Returns:
      true if a matching value was found, or false otherwise
    • getChangeStreamSessionTransactionId

      public static SourceInfo.SessionTransactionId getChangeStreamSessionTransactionId(com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.BsonDocument> event)
      Helper function to extract the session transaction-id from an Change Stream event.
      Parameters:
      event - the Change Stream event
      Returns:
      the session transaction id from the event
    • clusterDescription

      public static com.mongodb.connection.ClusterDescription clusterDescription(com.mongodb.client.MongoClient client)
      Retrieves cluster description, forcing a connection if not yet available
      Parameters:
      client - cluster connection client
      Returns:
      cluster description
    • replicaSetName

      public static Optional<String> replicaSetName(com.mongodb.connection.ClusterDescription clusterDescription)