Class MongoUtil


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

      Constructors 
      Modifier Constructor Description
      private MongoUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      static String getOplogSessionTransactionId​(org.bson.Document oplogEvent)
      Helper function to extract the session transaction-id from an oplog event.
      protected static com.mongodb.ServerAddress getPrimaryAddress​(com.mongodb.client.MongoClient client)  
      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.
      static com.mongodb.ServerAddress parseAddress​(String addressStr)
      Parse the server address string, of the form host:port or host.
      protected static List<com.mongodb.ServerAddress> parseAddresses​(String addressStr)
      Parse the comma-separated list of server addresses.
      static String replicaSetUsedIn​(String addresses)
      Find the name of the replica set precedes the host addresses.
      protected static String toString​(com.mongodb.ServerAddress address)  
      protected static String toString​(List<com.mongodb.ServerAddress> addresses)  
    • Field Detail

      • ADDRESS_DELIMITER

        private static final String ADDRESS_DELIMITER
        The delimiter used between addresses.
        See Also:
        Constant Field Values
      • ADDRESS_DELIMITER_PATTERN

        public static final Pattern ADDRESS_DELIMITER_PATTERN
      • ADDRESS_PATTERN

        private static final Pattern ADDRESS_PATTERN
        Regular expression that gets the host and (optional) port. The raw expression is ([^:]+)(:(\d+))?.
      • IPV6_ADDRESS_PATTERN

        private static final Pattern IPV6_ADDRESS_PATTERN
        Regular expression that gets the IPv6 host and (optional) port, where the IPv6 address must be surrounded by square brackets. The raw expression is (\[[^]]+\])(:(\d+))?.
    • Constructor Detail

      • MongoUtil

        private MongoUtil()
    • Method Detail

      • replicaSetUsedIn

        public static String replicaSetUsedIn​(String addresses)
        Find the name of the replica set precedes the host addresses.
        Parameters:
        addresses - the string containing the host addresses, of the form replicaSetName/...; may not be null
        Returns:
        the replica set name, or null if no replica set name is in the string
      • 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
      • parseAddress

        public static com.mongodb.ServerAddress parseAddress​(String addressStr)
        Parse the server address string, of the form host:port or host.

        The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.

        Parameters:
        addressStr - the string containing the host and port; may be null
        Returns:
        the server address, or null if the string did not contain a host or host:port pair
      • getOplogSessionTransactionId

        public static String getOplogSessionTransactionId​(org.bson.Document oplogEvent)
        Helper function to extract the session transaction-id from an oplog event.
        Parameters:
        oplogEvent - the oplog event
        Returns:
        the session transaction id from the oplog event
      • parseAddresses

        protected static List<com.mongodb.ServerAddress> parseAddresses​(String addressStr)
        Parse the comma-separated list of server addresses. The format of the supplied string is one of the following:
         replicaSetName/host:port
         replicaSetName/host:port,host2:port2
         replicaSetName/host:port,host2:port2,host3:port3
         host:port
         host:port,host2:port2
         host:port,host2:port2,host3:port3
         
        where replicaSetName is the name of the replica set, host contains the resolvable hostname or IP address of the server, and port is the integral port number. If the port is not provided, the default port is used. If neither the host or port are provided (or addressString is null), then an address will use the default host and default port.

        The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.

        This method does not use the replica set name.

        Parameters:
        addressStr - the string containing a comma-separated list of host and port pairs, optionally preceded by a replica set name
        Returns:
        the list of server addresses; never null, but possibly empty
      • toString

        protected static String toString​(com.mongodb.ServerAddress address)
      • toString

        protected static String toString​(List<com.mongodb.ServerAddress> addresses)
      • getPrimaryAddress

        protected static com.mongodb.ServerAddress getPrimaryAddress​(com.mongodb.client.MongoClient client)