public class MongoUtil extends Object
| Modifier and Type | Field and Description |
|---|---|
private static String |
ADDRESS_DELIMITER
The delimiter used between addresses.
|
private static Pattern |
ADDRESS_PATTERN
Regular expression that gets the host and (optional) port.
|
private static Pattern |
IPV6_ADDRESS_PATTERN
Regular expression that gets the IPv6 host and (optional) port, where the IPv6 address must be surrounded
by square brackets.
|
| Modifier | Constructor and Description |
|---|---|
private |
MongoUtil() |
| Modifier and Type | Method and 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.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.MongoClient client,
Consumer<String> operation)
Perform the given operation on each of the database names.
|
static void |
onCollection(com.mongodb.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.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.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(List<com.mongodb.ServerAddress> addresses) |
protected static String |
toString(com.mongodb.ServerAddress address) |
private static final String ADDRESS_DELIMITER
private static final Pattern ADDRESS_PATTERN
([^:]+)(:(\d+))?.private static final Pattern IPV6_ADDRESS_PATTERN
(\[[^]]+\])(:(\d+))?.public static String replicaSetUsedIn(String addresses)
addresses - the string containing the host addresses, of the form replicaSetName/...; may not be nullnull if no replica set name is in the stringpublic static void forEachDatabaseName(com.mongodb.MongoClient client,
Consumer<String> operation)
client - the MongoDB client; may not be nulloperation - the operation to perform; may not be nullpublic static void forEachCollectionNameInDatabase(com.mongodb.MongoClient client,
String databaseName,
Consumer<String> operation)
client - the MongoDB client; may not be nulldatabaseName - the name of the database; may not be nulloperation - the operation to perform; may not be nullpublic static <T> void forEach(com.mongodb.client.MongoIterable<T> iterable,
Consumer<T> operation)
iterable - the iterable collection obtained from a MongoDB client; may not be nulloperation - the operation to perform; may not be nullpublic static void onDatabase(com.mongodb.MongoClient client,
String dbName,
Consumer<com.mongodb.client.MongoDatabase> dbOperation)
client - the MongoDB client; may not be nulldbName - the name of the database; may not be nulldbOperation - the operation to perform; may not be nullpublic static void onCollection(com.mongodb.MongoClient client,
String dbName,
String collectionName,
Consumer<com.mongodb.client.MongoCollection<org.bson.Document>> collectionOperation)
client - the MongoDB client; may not be nulldbName - the name of the database; may not be nullcollectionName - the name of the collection; may not be nullcollectionOperation - the operation to perform; may not be nullpublic static void onCollectionDocuments(com.mongodb.MongoClient client,
String dbName,
String collectionName,
BlockingConsumer<org.bson.Document> documentOperation)
client - the MongoDB client; may not be nulldbName - the name of the database; may not be nullcollectionName - the name of the collection; may not be nulldocumentOperation - the operation to perform; may not be nullpublic static <T> boolean contains(com.mongodb.client.MongoIterable<String> iterable, String match)
MongoIterable contains an element that is equal to the supplied value.iterable - the iterable; may not be nullmatch - the value to find in the iterable; may be nulltrue if a matching value was found, or false otherwisepublic static <T> boolean contains(com.mongodb.client.MongoIterable<T> iterable,
Predicate<T> matcher)
MongoIterable contains at least one element that satisfies the given predicate.iterable - the iterable; may not be nullmatcher - the predicate function called on each value in the iterable until a match is found; may not be nulltrue if a matching value was found, or false otherwisepublic static com.mongodb.ServerAddress parseAddress(String addressStr)
host:port or host.
The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.
addressStr - the string containing the host and port; may be nullnull if the string did not contain a host or host:port pairprotected static List<com.mongodb.ServerAddress> parseAddresses(String addressStr)
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:port3where
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.
addressStr - the string containing a comma-separated list of host and port pairs, optionally preceded by a
replica set nameprotected static String toString(com.mongodb.ServerAddress address)
Copyright © 2017 JBoss by Red Hat. All rights reserved.