@@ -55,7 +55,7 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
5555 private static final String MAX_TIME = "maxTimeMS" ;
5656 private static final String HINT = "hint" ;
5757
58- private final boolean allowDiskUse ;
58+ private final Optional < Boolean > allowDiskUse ;
5959 private final boolean explain ;
6060 private final Optional <Document > cursor ;
6161 private final Optional <Collation > collation ;
@@ -123,10 +123,10 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
123123 * @param hint can be {@literal null}, used to provide an index that would be forcibly used by query optimizer.
124124 * @since 3.1
125125 */
126- private AggregationOptions (boolean allowDiskUse , boolean explain , @ Nullable Document cursor ,
126+ private AggregationOptions (@ Nullable Boolean allowDiskUse , boolean explain , @ Nullable Document cursor ,
127127 @ Nullable Collation collation , @ Nullable String comment , @ Nullable Object hint ) {
128128
129- this .allowDiskUse = allowDiskUse ;
129+ this .allowDiskUse = Optional . ofNullable ( allowDiskUse ) ;
130130 this .explain = explain ;
131131 this .cursor = Optional .ofNullable (cursor );
132132 this .collation = Optional .ofNullable (collation );
@@ -159,7 +159,7 @@ public static AggregationOptions fromDocument(Document document) {
159159
160160 Assert .notNull (document , "Document must not be null" );
161161
162- boolean allowDiskUse = document .getBoolean (ALLOW_DISK_USE , false );
162+ Boolean allowDiskUse = document .get (ALLOW_DISK_USE , Boolean . class );
163163 boolean explain = document .getBoolean (EXPLAIN , false );
164164 Document cursor = document .get (CURSOR , Document .class );
165165 Collation collation = document .containsKey (COLLATION ) ? Collation .from (document .get (COLLATION , Document .class ))
@@ -185,13 +185,23 @@ public static Builder builder() {
185185 }
186186
187187 /**
188- * Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp subdirectory in
189- * the dbPath directory.
188+ * Enables writing to temporary files. When set to {@literal true} , aggregation stages can write data to the
189+ * {@code _tmp} subdirectory in the {@code dbPath} directory.
190190 *
191- * @return {@literal true} if enabled.
191+ * @return {@literal true} if enabled; {@literal false} otherwise (or if not set) .
192192 */
193193 public boolean isAllowDiskUse () {
194- return allowDiskUse ;
194+ return allowDiskUse .orElse (false );
195+ }
196+
197+ /**
198+ * Return whether {@link #isAllowDiskUse} is configured.
199+ *
200+ * @return {@literal true} if is {@code allowDiskUse} is configured, {@literal false} otherwise.
201+ * @since 4.2.5
202+ */
203+ public boolean isAllowDiskUseSet () {
204+ return allowDiskUse .isPresent ();
195205 }
196206
197207 /**
@@ -335,8 +345,8 @@ Document applyAndReturnPotentiallyChangedCommand(Document command) {
335345
336346 Document result = new Document (command );
337347
338- if (allowDiskUse && !result .containsKey (ALLOW_DISK_USE )) {
339- result .put (ALLOW_DISK_USE , allowDiskUse );
348+ if (isAllowDiskUseSet () && !result .containsKey (ALLOW_DISK_USE )) {
349+ result .put (ALLOW_DISK_USE , isAllowDiskUse () );
340350 }
341351
342352 if (explain && !result .containsKey (EXPLAIN )) {
@@ -370,7 +380,9 @@ Document applyAndReturnPotentiallyChangedCommand(Document command) {
370380 public Document toDocument () {
371381
372382 Document document = new Document ();
373- document .put (ALLOW_DISK_USE , allowDiskUse );
383+ if (isAllowDiskUseSet ()) {
384+ document .put (ALLOW_DISK_USE , isAllowDiskUse ());
385+ }
374386 document .put (EXPLAIN , explain );
375387
376388 cursor .ifPresent (val -> document .put (CURSOR , val ));
@@ -410,7 +422,7 @@ static Document createCursor(int cursorBatchSize) {
410422 */
411423 public static class Builder {
412424
413- private boolean allowDiskUse ;
425+ private Boolean allowDiskUse ;
414426 private boolean explain ;
415427 private @ Nullable Document cursor ;
416428 private @ Nullable Collation collation ;
0 commit comments