/*
 * 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.ExplainVerbosity;
import com.mongodb.MongoNamespace;
import com.mongodb.ReadConcern;
import com.mongodb.WriteConcern;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.binding.AsyncWriteBinding;
import com.mongodb.binding.WriteBinding;
import com.mongodb.client.model.AggregationLevel;
import com.mongodb.client.model.Collation;
import com.mongodb.connection.AsyncConnection;
import com.mongodb.connection.Connection;
import com.mongodb.connection.ConnectionDescription;
import org.bson.BsonArray;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonInt64;
import org.bson.BsonString;
import org.bson.BsonValue;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.mongodb.assertions.Assertions.isTrueArgument;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotFour;
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotSix;
import static com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotTwo;
import static com.mongodb.internal.operation.WriteConcernHelper.appendWriteConcernToCommand;
import static com.mongodb.operation.CommandOperationHelper.executeCommand;
import static com.mongodb.operation.CommandOperationHelper.executeCommandAsync;
import static com.mongodb.operation.CommandOperationHelper.writeConcernErrorTransformer;
import static com.mongodb.operation.CommandOperationHelper.writeConcernErrorWriteTransformer;
import static com.mongodb.operation.OperationHelper.AsyncCallableWithConnection;
import static com.mongodb.operation.OperationHelper.CallableWithConnection;
import static com.mongodb.operation.OperationHelper.LOGGER;
import static com.mongodb.operation.OperationHelper.releasingCallback;
import static com.mongodb.operation.OperationHelper.validateCollation;
import static com.mongodb.operation.OperationHelper.withConnection;
import static com.mongodb.operation.OperationHelper.withAsyncConnection;

An operation that executes an aggregation that writes its results to a collection (which is what makes this a write operation rather than a read operation).
@mongodb.server.release2.6
@mongodb.driver.manualreference/command/aggregate/ Aggregation
Since:3.0
/** * An operation that executes an aggregation that writes its results to a collection (which is what makes this a write operation rather than * a read operation). * * @mongodb.server.release 2.6 * @mongodb.driver.manual reference/command/aggregate/ Aggregation * @since 3.0 */
@Deprecated public class AggregateToCollectionOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> { private final MongoNamespace namespace; private final List<BsonDocument> pipeline; private final WriteConcern writeConcern; private final ReadConcern readConcern; private final AggregationLevel aggregationLevel; private Boolean allowDiskUse; private long maxTimeMS; private Boolean bypassDocumentValidation; private Collation collation; private String comment; private BsonDocument hint;
Construct a new instance.
Params:
  • namespace – the database and collection namespace for the operation.
  • pipeline – the aggregation pipeline.
Deprecated:Prefer AggregateToCollectionOperation(MongoNamespace, List<BsonDocument>, WriteConcern)
/** * Construct a new instance. * * @param namespace the database and collection namespace for the operation. * @param pipeline the aggregation pipeline. * @deprecated Prefer {@link #AggregateToCollectionOperation(MongoNamespace, List, WriteConcern)} */
@Deprecated public AggregateToCollectionOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline) { this(namespace, pipeline, null, null, AggregationLevel.COLLECTION); }
Construct a new instance.
Params:
  • namespace – the database and collection namespace for the operation.
  • pipeline – the aggregation pipeline.
  • writeConcern – the write concern to apply
Since:3.4
/** * Construct a new instance. * * @param namespace the database and collection namespace for the operation. * @param pipeline the aggregation pipeline. * @param writeConcern the write concern to apply * * @since 3.4 */
public AggregateToCollectionOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline, final WriteConcern writeConcern) { this(namespace, pipeline, null, writeConcern, AggregationLevel.COLLECTION); }
Construct a new instance.
Params:
  • namespace – the database and collection namespace for the operation.
  • pipeline – the aggregation pipeline.
  • readConcern – the read concern to apply
Since:3.11
/** * Construct a new instance. * * @param namespace the database and collection namespace for the operation. * @param pipeline the aggregation pipeline. * @param readConcern the read concern to apply * * @since 3.11 */
public AggregateToCollectionOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline, final ReadConcern readConcern) { this(namespace, pipeline, readConcern, null, AggregationLevel.COLLECTION); }
Construct a new instance.
Params:
  • namespace – the database and collection namespace for the operation.
  • pipeline – the aggregation pipeline.
  • writeConcern – the write concern to apply
  • readConcern – the read concern to apply
Since:3.11
/** * Construct a new instance. * * @param namespace the database and collection namespace for the operation. * @param pipeline the aggregation pipeline. * @param writeConcern the write concern to apply * @param readConcern the read concern to apply * @since 3.11 */
public AggregateToCollectionOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline, final ReadConcern readConcern, final WriteConcern writeConcern) { this(namespace, pipeline, readConcern, writeConcern, AggregationLevel.COLLECTION); }
Construct a new instance.
Params:
  • namespace – the database and collection namespace for the operation.
  • pipeline – the aggregation pipeline.
  • writeConcern – the write concern to apply
  • aggregationLevel – the aggregation level
Since:3.10
/** * Construct a new instance. * * @param namespace the database and collection namespace for the operation. * @param pipeline the aggregation pipeline. * @param writeConcern the write concern to apply * @param aggregationLevel the aggregation level * @since 3.10 */
public AggregateToCollectionOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline, final WriteConcern writeConcern, final AggregationLevel aggregationLevel) { this(namespace, pipeline, ReadConcern.DEFAULT, writeConcern, aggregationLevel); }
Construct a new instance.
Params:
  • namespace – the database and collection namespace for the operation.
  • pipeline – the aggregation pipeline.
  • readConcern – the read concern to apply
  • writeConcern – the write concern to apply
  • aggregationLevel – the aggregation level
Since:3.11
/** * Construct a new instance. * * @param namespace the database and collection namespace for the operation. * @param pipeline the aggregation pipeline. * @param readConcern the read concern to apply * @param writeConcern the write concern to apply * @param aggregationLevel the aggregation level * @since 3.11 */
public AggregateToCollectionOperation(final MongoNamespace namespace, final List<BsonDocument> pipeline, final ReadConcern readConcern, final WriteConcern writeConcern, final AggregationLevel aggregationLevel) { this.namespace = notNull("namespace", namespace); this.pipeline = notNull("pipeline", pipeline); this.writeConcern = writeConcern; this.readConcern = readConcern; this.aggregationLevel = notNull("aggregationLevel", aggregationLevel); isTrueArgument("pipeline is not empty", !pipeline.isEmpty()); }
Gets the aggregation pipeline.
Returns:the pipeline
@mongodb.driver.manualcore/aggregation-introduction/#aggregation-pipelines Aggregation Pipeline
/** * Gets the aggregation pipeline. * * @return the pipeline * @mongodb.driver.manual core/aggregation-introduction/#aggregation-pipelines Aggregation Pipeline */
public List<BsonDocument> getPipeline() { return pipeline; }
Gets the read concern.
Returns:the read concern, which may be null
Since:3.11
/** * Gets the read concern. * * @return the read concern, which may be null * * @since 3.11 */
public ReadConcern getReadConcern() { return readConcern; }
Gets the write concern.
Returns:the write concern, which may be null
Since:3.4
/** * Gets the write concern. * * @return the write concern, which may be null * * @since 3.4 */
public WriteConcern getWriteConcern() { return writeConcern; }
Whether writing to temporary files is enabled. A null value indicates that it's unspecified.
Returns:true if writing to temporary files is enabled
@mongodb.driver.manualreference/command/aggregate/ Aggregation
@mongodb.server.release2.6
/** * Whether writing to temporary files is enabled. A null value indicates that it's unspecified. * * @return true if writing to temporary files is enabled * @mongodb.driver.manual reference/command/aggregate/ Aggregation * @mongodb.server.release 2.6 */
public Boolean getAllowDiskUse() { return allowDiskUse; }
Enables writing to temporary files. A null value indicates that it's unspecified.
Params:
  • allowDiskUse – true if writing to temporary files is enabled
Returns:this
@mongodb.driver.manualreference/command/aggregate/ Aggregation
@mongodb.server.release2.6
/** * Enables writing to temporary files. A null value indicates that it's unspecified. * * @param allowDiskUse true if writing to temporary files is enabled * @return this * @mongodb.driver.manual reference/command/aggregate/ Aggregation * @mongodb.server.release 2.6 */
public AggregateToCollectionOperation allowDiskUse(final Boolean allowDiskUse) { this.allowDiskUse = allowDiskUse; return this; }
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/method/cursor.maxTimeMS/#cursor.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/method/cursor.maxTimeMS/#cursor.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/method/cursor.maxTimeMS/#cursor.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/method/cursor.maxTimeMS/#cursor.maxTimeMS Max Time */
public AggregateToCollectionOperation maxTime(final long maxTime, final TimeUnit timeUnit) { notNull("timeUnit", timeUnit); this.maxTimeMS = TimeUnit.MILLISECONDS.convert(maxTime, timeUnit); return this; }
Gets the bypass document level validation flag
Returns:the bypass document level validation flag
Since:3.2
/** * Gets the bypass document level validation flag * * @return the bypass document level validation flag * @since 3.2 */
public Boolean getBypassDocumentValidation() { return bypassDocumentValidation; }
Sets the bypass document level validation flag.

Note: This only applies when an $out or $merge stage is specified

.
Params:
  • bypassDocumentValidation – If true, allows the write to opt-out of document level validation.
Returns:this
Since:3.2
@mongodb.server.release3.2
/** * Sets the bypass document level validation flag. * * <p>Note: This only applies when an $out or $merge stage is specified</p>. * * @param bypassDocumentValidation If true, allows the write to opt-out of document level validation. * @return this * @since 3.2 * @mongodb.server.release 3.2 */
public AggregateToCollectionOperation bypassDocumentValidation(final Boolean bypassDocumentValidation) { this.bypassDocumentValidation = bypassDocumentValidation; return this; }
Returns the collation options
Returns:the collation options
Since:3.4
@mongodb.server.release3.4
/** * Returns the collation options * * @return the collation options * @since 3.4 * @mongodb.server.release 3.4 */
public Collation getCollation() { return collation; }
Sets the collation options

A null value represents the server default.

Params:
  • collation – the collation options to use
Returns:this
Since:3.4
@mongodb.server.release3.4
/** * Sets the collation options * * <p>A null value represents the server default.</p> * @param collation the collation options to use * @return this * @since 3.4 * @mongodb.server.release 3.4 */
public AggregateToCollectionOperation collation(final Collation collation) { this.collation = collation; return this; }
Returns the comment to send with the aggregate. The default is not to include a comment with the aggregation.
Returns:the comment
Since:3.6
@mongodb.server.release3.6
/** * Returns the comment to send with the aggregate. The default is not to include a comment with the aggregation. * * @return the comment * @since 3.6 * @mongodb.server.release 3.6 */
public String getComment() { return comment; }
Sets the comment to the aggregation. A null value means no comment is set.
Params:
  • comment – the comment
Returns:this
Since:3.6
@mongodb.server.release3.6
/** * Sets the comment to the aggregation. A null value means no comment is set. * * @param comment the comment * @return this * @since 3.6 * @mongodb.server.release 3.6 */
public AggregateToCollectionOperation comment(final String comment) { this.comment = comment; return this; }
Returns the hint for which index to use. The default is not to set a hint.
Returns:the hint
Since:3.6
@mongodb.server.release3.6
/** * Returns the hint for which index to use. The default is not to set a hint. * * @return the hint * @since 3.6 * @mongodb.server.release 3.6 */
public BsonDocument getHint() { return hint; }
Sets the hint for which index to use. A null value means no hint is set.
Params:
  • hint – the hint
Returns:this
Since:3.6
@mongodb.server.release3.6
/** * Sets the hint for which index to use. A null value means no hint is set. * * @param hint the hint * @return this * @since 3.6 * @mongodb.server.release 3.6 */
public AggregateToCollectionOperation hint(final BsonDocument hint) { this.hint = hint; return this; }
Gets an operation whose execution explains this operation.
Params:
  • explainVerbosity – the explain verbosity
Returns:a read operation that when executed will explain this operation
/** * Gets an operation whose execution explains this operation. * * @param explainVerbosity the explain verbosity * @return a read operation that when executed will explain this operation */
public ReadOperation<BsonDocument> asExplainableOperation(final ExplainVerbosity explainVerbosity) { return new AggregateExplainOperation(namespace, pipeline) .allowDiskUse(allowDiskUse) .maxTime(maxTimeMS, TimeUnit.MILLISECONDS) .hint(hint); } @Override public Void execute(final WriteBinding binding) { return withConnection(binding, new CallableWithConnection<Void>() { @Override public Void call(final Connection connection) { validateCollation(connection, collation); return executeCommand(binding, namespace.getDatabaseName(), getCommand(connection.getDescription()), connection, writeConcernErrorTransformer()); } }); } @Override public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<Void> callback) { withAsyncConnection(binding, new AsyncCallableWithConnection() { @Override public void call(final AsyncConnection connection, final Throwable t) { SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER); if (t != null) { errHandlingCallback.onResult(null, t); } else { final SingleResultCallback<Void> wrappedCallback = releasingCallback(errHandlingCallback, connection); validateCollation(connection, collation, new AsyncCallableWithConnection() { @Override public void call(final AsyncConnection connection, final Throwable t) { if (t != null) { wrappedCallback.onResult(null, t); } else { executeCommandAsync(binding, namespace.getDatabaseName(), getCommand(connection.getDescription()), connection, writeConcernErrorWriteTransformer(), wrappedCallback); } } }); } } }); } private BsonDocument getCommand(final ConnectionDescription description) { BsonValue aggregationTarget = (aggregationLevel == AggregationLevel.DATABASE) ? new BsonInt32(1) : new BsonString(namespace.getCollectionName()); BsonDocument commandDocument = new BsonDocument("aggregate", aggregationTarget); commandDocument.put("pipeline", new BsonArray(pipeline)); if (maxTimeMS > 0) { commandDocument.put("maxTimeMS", new BsonInt64(maxTimeMS)); } if (allowDiskUse != null) { commandDocument.put("allowDiskUse", BsonBoolean.valueOf(allowDiskUse)); } if (bypassDocumentValidation != null && serverIsAtLeastVersionThreeDotTwo(description)) { commandDocument.put("bypassDocumentValidation", BsonBoolean.valueOf(bypassDocumentValidation)); } if (serverIsAtLeastVersionThreeDotSix(description)) { commandDocument.put("cursor", new BsonDocument()); } appendWriteConcernToCommand(writeConcern, commandDocument, description); if (readConcern != null && !readConcern.isServerDefault() && serverIsAtLeastVersionThreeDotFour(description)) { commandDocument.put("readConcern", readConcern.asDocument()); } if (collation != null) { commandDocument.put("collation", collation.asDocument()); } if (comment != null) { commandDocument.put("comment", new BsonString(comment)); } if (hint != null) { commandDocument.put("hint", hint); } return commandDocument; } }