/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.operation;

import com.mongodb.async.AsyncBatchCursor;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.binding.AsyncConnectionSource;
import com.mongodb.binding.AsyncReadBinding;
import com.mongodb.binding.ConnectionSource;
import com.mongodb.binding.ReadBinding;
import com.mongodb.connection.AsyncConnection;
import com.mongodb.connection.Connection;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.connection.QueryResult;
import com.mongodb.connection.ServerDescription;
import com.mongodb.operation.CommandOperationHelper.CommandReadTransformer;
import com.mongodb.operation.CommandOperationHelper.CommandReadTransformerAsync;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonInt64;
import org.bson.codecs.Decoder;

import java.util.concurrent.TimeUnit;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.operation.CommandOperationHelper.CommandCreator;
import static com.mongodb.operation.CommandOperationHelper.executeCommand;
import static com.mongodb.operation.CommandOperationHelper.executeCommandAsync;
import static com.mongodb.operation.OperationHelper.LOGGER;


An operation that provides a cursor allowing iteration through the metadata of all the databases for a MongoClient.
Type parameters:
  • <T> – the document type
Since:3.0
/** * An operation that provides a cursor allowing iteration through the metadata of all the databases for a MongoClient. * * @param <T> the document type * @since 3.0 */
@Deprecated public class ListDatabasesOperation<T> implements AsyncReadOperation<AsyncBatchCursor<T>>, ReadOperation<BatchCursor<T>> { private final Decoder<T> decoder; private boolean retryReads; private long maxTimeMS; private BsonDocument filter; private Boolean nameOnly;
Construct a new instance.
Params:
  • decoder – the decoder to use for the results
/** * Construct a new instance. * * @param decoder the decoder to use for the results */
public ListDatabasesOperation(final Decoder<T> decoder) { this.decoder = notNull("decoder", decoder); }
Gets the maximum execution time on the server for this operation. The default is 0, which places no limit on the execution time.
Params:
  • timeUnit – the time unit to return the result in
Returns:the maximum execution time in the given time unit
@mongodb.driver.manualreference/operator/meta/maxTimeMS/ Max Time
/** * Gets the maximum execution time on the server for this operation. The default is 0, which places no limit on the execution time. * * @param timeUnit the time unit to return the result in * @return the maximum execution time in the given time unit * @mongodb.driver.manual reference/operator/meta/maxTimeMS/ Max Time */
public long getMaxTime(final TimeUnit timeUnit) { notNull("timeUnit", timeUnit); return timeUnit.convert(maxTimeMS, TimeUnit.MILLISECONDS); }
Sets the maximum execution time on the server for this operation.
Params:
  • maxTime – the max time
  • timeUnit – the time unit, which may not be null
Returns:this
@mongodb.driver.manualreference/operator/meta/maxTimeMS/ Max Time
/** * Sets the maximum execution time on the server for this operation. * * @param maxTime the max time * @param timeUnit the time unit, which may not be null * @return this * @mongodb.driver.manual reference/operator/meta/maxTimeMS/ Max Time */
public ListDatabasesOperation<T> maxTime(final long maxTime, final TimeUnit timeUnit) { notNull("timeUnit", timeUnit); this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit); return this; }
Sets the query filter to apply to the returned database names.
Params:
  • filter – the filter, which may be null.
Returns:this
Since:3.6
@mongodb.server.release3.6
/** * Sets the query filter to apply to the returned database names. * * @param filter the filter, which may be null. * @return this * @since 3.6 * @mongodb.server.release 3.6 */
public ListDatabasesOperation<T> filter(final BsonDocument filter) { this.filter = filter; return this; }
Gets the query filter to apply to the returned database names.
Returns:this
Since:3.6
@mongodb.server.release3.6
/** * Gets the query filter to apply to the returned database names. * * @return this * @since 3.6 * @mongodb.server.release 3.6 */
public BsonDocument getFilter() { return filter; }
Sets the nameOnly flag that indicates whether the command should return just the database names or return the database names and size information.
Params:
  • nameOnly – the nameOnly flag, which may be null
Returns:this
Since:3.6
@mongodb.server.release3.6
/** * Sets the nameOnly flag that indicates whether the command should return just the database names or return the database names and * size information. * * @param nameOnly the nameOnly flag, which may be null * @return this * @since 3.6 * @mongodb.server.release 3.6 */
public ListDatabasesOperation<T> nameOnly(final Boolean nameOnly) { this.nameOnly = nameOnly; return this; }
Enables retryable reads if a read fails due to a network error.
Params:
  • retryReads – true if reads should be retried
Returns:this
Since:3.11
/** * Enables retryable reads if a read fails due to a network error. * * @param retryReads true if reads should be retried * @return this * @since 3.11 */
public ListDatabasesOperation<T> retryReads(final boolean retryReads) { this.retryReads = retryReads; return this; }
Gets the value for retryable reads. The default is true.
Returns:the retryable reads value
Since:3.11
/** * Gets the value for retryable reads. The default is true. * * @return the retryable reads value * @since 3.11 */
public boolean getRetryReads() { return retryReads; }
Gets the nameOnly flag that indicates whether the command should return just the database names or return the database names and size information.
Returns:this
Since:3.6
@mongodb.server.release3.6
/** * Gets the nameOnly flag that indicates whether the command should return just the database names or return the database names and * size information. * * @return this * @since 3.6 * @mongodb.server.release 3.6 */
public Boolean getNameOnly() { return nameOnly; }
Executing this will return a list of all the databases names in the MongoDB instance.
Params:
  • binding – the binding.
Returns:a List of Strings of the names of all the databases in the MongoDB instance.
/** * Executing this will return a list of all the databases names in the MongoDB instance. * * @param binding the binding. * @return a List of Strings of the names of all the databases in the MongoDB instance. */
@Override public BatchCursor<T> execute(final ReadBinding binding) { return executeCommand(binding, "admin", getCommandCreator(), CommandResultDocumentCodec.create(decoder, "databases"), transformer(), retryReads); } @Override public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<AsyncBatchCursor<T>> callback) { executeCommandAsync(binding, "admin", getCommandCreator(), CommandResultDocumentCodec.create(decoder, "databases"), asyncTransformer(), retryReads, errorHandlingCallback(callback, LOGGER)); } private CommandReadTransformer<BsonDocument, BatchCursor<T>> transformer() { return new CommandReadTransformer<BsonDocument, BatchCursor<T>>() { @Override public BatchCursor<T> apply(final BsonDocument result, final ConnectionSource source, final Connection connection) { return new QueryBatchCursor<T>(createQueryResult(result, connection.getDescription()), 0, 0, decoder, source); } }; } private CommandReadTransformerAsync<BsonDocument, AsyncBatchCursor<T>> asyncTransformer() { return new CommandReadTransformerAsync<BsonDocument, AsyncBatchCursor<T>>() { @Override public AsyncBatchCursor<T> apply(final BsonDocument result, final AsyncConnectionSource source, final AsyncConnection connection) { return new AsyncQueryBatchCursor<T>(createQueryResult(result, connection.getDescription()), 0, 0, 0, decoder, source, connection, result); } }; } @SuppressWarnings("unchecked") private QueryResult<T> createQueryResult(final BsonDocument result, final ConnectionDescription description) { return new QueryResult<T>(null, BsonDocumentWrapperHelper.<T>toList(result, "databases"), 0, description.getServerAddress()); } private CommandCreator getCommandCreator() { return new CommandCreator() { @Override public BsonDocument create(final ServerDescription serverDescription, final ConnectionDescription connectionDescription) { return getCommand(); } }; } private BsonDocument getCommand() { BsonDocument command = new BsonDocument("listDatabases", new BsonInt32(1)); if (maxTimeMS > 0) { command.put("maxTimeMS", new BsonInt64(maxTimeMS)); } if (filter != null) { command.put("filter", filter); } if (nameOnly != null) { command.put("nameOnly", new BsonBoolean(nameOnly)); } return command; } }