internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
        {
            var serverVersion = connectionDescription.ServerVersion;

            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            var readConcern = _readConcern != null
                ? ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern)
                : null;

            var writeConcern = WriteConcernHelper.GetWriteConcernForCommandThatWrites(session, _writeConcern, serverVersion);

            return(new BsonDocument
            {
                { "aggregate", _collectionNamespace == null ? (BsonValue)1 : _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(_pipeline) },
                { "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
                { "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue&& Feature.BypassDocumentValidation.IsSupported(serverVersion) },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "readConcern", readConcern, readConcern != null },
                { "writeConcern", writeConcern, writeConcern != null },
                { "cursor", new BsonDocument(), serverVersion >= new SemanticVersion(3, 6, 0) },
                { "hint", () => _hint, _hint != null },
                { "let", () => _let, _let != null },
                { "comment", () => _comment, _comment != null }
            });
        }
Beispiel #2
0
        // methods
        internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
        {
            var serverVersion = connectionDescription.ServerVersion;

            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            var flags        = GetFlags();
            var writeConcern = WriteConcernHelper.GetWriteConcernForCommandThatWrites(session, _writeConcern, serverVersion);

            return(new BsonDocument
            {
                { "create", _collectionNamespace.CollectionName },
                { "capped", () => _capped.Value, _capped.HasValue },
                { "autoIndexId", () => _autoIndexId.Value, _autoIndexId.HasValue },
                { "size", () => _maxSize.Value, _maxSize.HasValue },
                { "max", () => _maxDocuments.Value, _maxDocuments.HasValue },
                { "flags", () => (int)flags.Value, flags.HasValue },
                { "storageEngine", () => _storageEngine, _storageEngine != null },
                { "indexOptionDefaults", _indexOptionDefaults, _indexOptionDefaults != null },
                { "validator", _validator, _validator != null },
                { "validationAction", () => _validationAction.Value.ToString().ToLowerInvariant(), _validationAction.HasValue },
                { "validationLevel", () => _validationLevel.Value.ToString().ToLowerInvariant(), _validationLevel.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "writeConcern", writeConcern, writeConcern != null }
            });
        }
Beispiel #3
0
        // methods
        internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
        {
            var flags        = GetFlags();
            var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);

            return(new BsonDocument
            {
                { "create", _collectionNamespace.CollectionName },
                { "capped", () => _capped.Value, _capped.HasValue },
                { "autoIndexId", () => _autoIndexId.Value, _autoIndexId.HasValue },
                { "size", () => _maxSize.Value, _maxSize.HasValue },
                { "max", () => _maxDocuments.Value, _maxDocuments.HasValue },
                { "flags", () => (int)flags.Value, flags.HasValue },
                { "storageEngine", _storageEngine, _storageEngine != null },
                { "indexOptionDefaults", _indexOptionDefaults, _indexOptionDefaults != null },
                { "validator", _validator, _validator != null },
                { "validationAction", () => _validationAction.Value.ToString().ToLowerInvariant(), _validationAction.HasValue },
                { "validationLevel", () => _validationLevel.Value.ToString().ToLowerInvariant(), _validationLevel.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "comment", _comment, _comment != null },
                { "writeConcern", writeConcern, writeConcern != null },
                { "expireAfterSeconds", () => _expireAfter.Value.TotalSeconds, _expireAfter.HasValue },
                { "timeseries", () => _timeSeriesOptions.ToBsonDocument(), _timeSeriesOptions != null }
            });
        }
Beispiel #4
0
        // methods
        internal BsonDocument CreateIndexDocument(SemanticVersion serverVersion)
        {
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            var document = new BsonDocument
            {
                { "key", _keys },
                { "name", GetIndexName() },
                { "background", () => _background.Value, _background.HasValue },
                { "bits", () => _bits.Value, _bits.HasValue },
                { "bucketSize", () => _bucketSize.Value, _bucketSize.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "default_language", () => _defaultLanguage, _defaultLanguage != null },
                { "expireAfterSeconds", () => _expireAfter.Value.TotalSeconds, _expireAfter.HasValue },
                { "language_override", () => _languageOverride, _languageOverride != null },
                { "max", () => _max.Value, _max.HasValue },
                { "min", () => _min.Value, _min.HasValue },
                { "partialFilterExpression", _partialFilterExpression, _partialFilterExpression != null },
                { "sparse", () => _sparse.Value, _sparse.HasValue },
                { "2dsphereIndexVersion", () => _sphereIndexVersion.Value, _sphereIndexVersion.HasValue },
                { "storageEngine", () => _storageEngine, _storageEngine != null },
                { "textIndexVersion", () => _textIndexVersion.Value, _textIndexVersion.HasValue },
                { "unique", () => _unique.Value, _unique.HasValue },
                { "v", () => _version.Value, _version.HasValue },
                { "weights", () => _weights, _weights != null }
            };

            if (_additionalOptions != null)
            {
                document.Merge(_additionalOptions, overwriteExistingElements: false);
            }
            return(document);
        }
Beispiel #5
0
        // methods
        internal override BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription, long?transactionNumber)
        {
            var serverVersion = connectionDescription.ServerVersion;

            Feature.Collation.ThrowIfNotSupported(serverVersion, Collation);

            var writeConcern = WriteConcernHelper.GetWriteConcernForCommand(session, WriteConcern, serverVersion, Feature.FindAndModifyWriteConcern);

            return(new BsonDocument
            {
                { "findAndModify", CollectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _update },
                { "new", true, _returnDocument == ReturnDocument.After },
                { "sort", _sort, _sort != null },
                { "fields", _projection, _projection != null },
                { "upsert", true, _isUpsert },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "writeConcern", writeConcern, writeConcern != null },
                { "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue&& Feature.BypassDocumentValidation.IsSupported(serverVersion) },
                { "collation", () => Collation.ToBsonDocument(), Collation != null },
                { "arrayFilters", () => new BsonArray(_arrayFilters), _arrayFilters != null },
                { "txnNumber", () => transactionNumber, transactionNumber.HasValue }
            });
        }
        // methods
        internal override BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription, long?transactionNumber)
        {
            var maxWireVersion = connectionDescription.MaxWireVersion;

            if (Feature.HintForFindAndModifyFeature.DriverMustThrowIfNotSupported(maxWireVersion) || (WriteConcern != null && !WriteConcern.IsAcknowledged))
            {
                if (_hint != null)
                {
                    throw new NotSupportedException($"Server version {WireVersion.GetServerVersionForErrorMessage(maxWireVersion)} does not support hints.");
                }
            }

            var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, WriteConcern);

            return(new BsonDocument
            {
                { "findAndModify", CollectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "sort", _sort, _sort != null },
                { "fields", _projection, _projection != null },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "writeConcern", writeConcern, writeConcern != null },
                { "collation", () => Collation.ToBsonDocument(), Collation != null },
                { "hint", _hint, _hint != null },
                { "txnNumber", () => transactionNumber, transactionNumber.HasValue },
                { "let", _let, _let != null }
            });
        }
        // methods
        internal override BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription, long?transactionNumber)
        {
            var serverVersion = connectionDescription.ServerVersion;

            if (Feature.HintForFindAndModifyFeature.DriverMustThrowIfNotSupported(serverVersion) || (WriteConcern != null && !WriteConcern.IsAcknowledged))
            {
                if (_hint != null)
                {
                    throw new NotSupportedException($"Server version {serverVersion} does not support hints.");
                }
            }

            var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, WriteConcern);

            return(new BsonDocument
            {
                { "findAndModify", CollectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _replacement },
                { "new", true, _returnDocument == ReturnDocument.After },
                { "sort", _sort, _sort != null },
                { "fields", _projection, _projection != null },
                { "upsert", true, _isUpsert },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "writeConcern", writeConcern, writeConcern != null },
                { "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue },
                { "collation", () => Collation.ToBsonDocument(), Collation != null },
                { "hint", () => _hint, _hint != null },
                { "txnNumber", () => transactionNumber, transactionNumber.HasValue }
            });
        }
Beispiel #8
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnCollation_Click(object sender, EventArgs e)
 {
     var frm = new frmCreateCollation();
     UIAssistant.OpenModalForm(frm, false, true);
     if (frm.mCollation != null)
     {
         mCollation = frm.mCollation;
         UiHelper.FillDataToTreeView("Collation", trvCollation, mCollation.ToBsonDocument());
     }
 }
Beispiel #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCollation_Click(object sender, EventArgs e)
        {
            var frm = new frmCreateCollation();

            UIAssistant.OpenModalForm(frm, false, true);
            if (frm.mCollation != null)
            {
                mCollation = frm.mCollation;
                UiHelper.FillDataToTreeView("Collation", trvCollation, mCollation.ToBsonDocument());
            }
        }
Beispiel #10
0
        // private methods
        internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
        {
            var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);

            return(new BsonDocument
            {
                { "create", _viewName },
                { "viewOn", _viewOn },
                { "pipeline", new BsonArray(_pipeline) },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "writeConcern", writeConcern, writeConcern != null }
            });
        }
        /// <summary>
        /// Sets the collation.
        /// </summary>
        /// <param name="collation">The collation.</param>
        /// <returns>The builder (so method calls can be chained).</returns>
        public CreateViewOptionsBuilder SetCollation(Collation collation)
        {
            if (collation == null)
            {
                _document.Remove("collation");
            }
            else
            {
                _document["collation"] = collation.ToBsonDocument();
            }

            return(this);
        }
        // methods
        internal BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            return(new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "explain", true },
                { "pipeline", new BsonArray(_pipeline) },
                { "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null }
            });
        }
        // private methods
        internal BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.Views.ThrowIfNotSupported(serverVersion);
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            return(new BsonDocument
            {
                { "create", _viewName },
                { "viewOn", _viewOn },
                { "pipeline", new BsonArray(_pipeline) },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) }
            });
        }
 // methods
 internal BsonDocument CreateCommand()
 {
     return(new BsonDocument
     {
         { "aggregate", _collectionNamespace.CollectionName },
         { "explain", true },
         { "pipeline", new BsonArray(_pipeline) },
         { "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
         { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
         { "collation", () => _collation.ToBsonDocument(), _collation != null },
         { "hint", () => _hint, _hint != null },
         { "comment", _comment, _comment != null }
     });
 }
Beispiel #15
0
        internal BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            return(new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "pipeline", new BsonArray(_pipeline) },
                { "allowDiskUse", () => _allowDiskUse.Value, _allowDiskUse.HasValue },
                { "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue&& Feature.BypassDocumentValidation.IsSupported(serverVersion) },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) }
            });
        }
Beispiel #16
0
        // private methods
        internal BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.ReadConcern.ThrowIfNotSupported(serverVersion, _readConcern);
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            return(new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "query", _filter, _filter != null },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "readConcern", () => _readConcern.ToBsonDocument(), !_readConcern.IsServerDefault },
                { "collation", () => _collation.ToBsonDocument(), _collation != null }
            });
        }
        // methods
        internal override BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.Collation.ThrowIfNotSupported(serverVersion, Collation);

            return(new BsonDocument
            {
                { "findAndModify", CollectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "sort", _sort, _sort != null },
                { "fields", _projection, _projection != null },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "writeConcern", () => WriteConcern.ToBsonDocument(), WriteConcern != null && !WriteConcern.IsServerDefault && Feature.FindAndModifyWriteConcern.IsSupported(serverVersion) },
                { "collation", () => Collation.ToBsonDocument(), Collation != null }
            });
        }
Beispiel #18
0
        // private methods
        internal BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
        {
            Feature.ReadConcern.ThrowIfNotSupported(connectionDescription.ServerVersion, _readConcern);
            Feature.Collation.ThrowIfNotSupported(connectionDescription.ServerVersion, _collation);

            var readConcern = ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern);

            return(new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "query", _filter, _filter != null },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "readConcern", readConcern, readConcern != null }
            });
        }
Beispiel #19
0
        // private methods
        internal BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
        {
            Feature.ReadConcern.ThrowIfNotSupported(connectionDescription.ServerVersion, _readConcern);
            Feature.Collation.ThrowIfNotSupported(connectionDescription.ServerVersion, _collation);

            var command = new BsonDocument
            {
                { "distinct", _collectionNamespace.CollectionName },
                { "key", _fieldName },
                { "query", _filter, _filter != null },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null }
            };

            ReadConcernHelper.AppendReadConcern(command, _readConcern, connectionDescription, session);
            return(command);
        }
 // private methods
 internal BsonDocument CreateCommand()
 {
     return(new BsonDocument
     {
         { "group", new BsonDocument
           {
               { "ns", _collectionNamespace.CollectionName },
               { "key", _key, _key != null },
               { "$keyf", _keyFunction, _keyFunction != null },
               { "$reduce", _reduceFunction },
               { "initial", _initial },
               { "cond", _filter, _filter != null },
               { "finalize", _finalizeFunction, _finalizeFunction != null },
               { "collation", () => _collation.ToBsonDocument(), _collation != null }
           } },
         { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue }
     });
 }
        // private methods
        internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
        {
            var serverVersion = connectionDescription.ServerVersion;

            Feature.Views.ThrowIfNotSupported(serverVersion);
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            var writeConcern = WriteConcernHelper.GetWriteConcernForCommandThatWrites(session, _writeConcern, serverVersion);

            return(new BsonDocument
            {
                { "create", _viewName },
                { "viewOn", _viewOn },
                { "pipeline", new BsonArray(_pipeline) },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "writeConcern", writeConcern, writeConcern != null }
            });
        }
 // methods
 /// <summary>
 /// Creates the command.
 /// </summary>
 /// <param name="session">The session.</param>
 /// <param name="connectionDescription">The connection description.</param>
 /// <returns>
 /// The command.
 /// </returns>
 protected internal virtual BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
 {
     return(new BsonDocument
     {
         { "mapReduce", _collectionNamespace.CollectionName },
         { "map", _mapFunction },
         { "reduce", _reduceFunction },
         { "out", CreateOutputOptions() },
         { "query", _filter, _filter != null },
         { "sort", _sort, _sort != null },
         { "limit", () => _limit.Value, _limit.HasValue },
         { "finalize", _finalizeFunction, _finalizeFunction != null },
         { "scope", _scope, _scope != null },
         { "jsMode", () => _javaScriptMode.Value, _javaScriptMode.HasValue },
         { "verbose", () => _verbose.Value, _verbose.HasValue },
         { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
         { "collation", () => _collation.ToBsonDocument(), _collation != null }
     });
 }
        // private methods
        internal BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            return(new BsonDocument
            {
                { "group", new BsonDocument
                  {
                      { "ns", _collectionNamespace.CollectionName },
                      { "key", _key, _key != null },
                      { "$keyf", _keyFunction, _keyFunction != null },
                      { "$reduce", _reduceFunction },
                      { "initial", _initial },
                      { "cond", _filter, _filter != null },
                      { "finalize", _finalizeFunction, _finalizeFunction != null },
                      { "collation", () => _collation.ToBsonDocument(), _collation != null }
                  } },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue }
            });
        }
Beispiel #24
0
        // methods
        internal override BsonDocument CreateCommand(SemanticVersion serverVersion, long?transactionNumber)
        {
            Feature.Collation.ThrowIfNotSupported(serverVersion, Collation);

            return(new BsonDocument
            {
                { "findAndModify", CollectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _replacement },
                { "new", true, _returnDocument == ReturnDocument.After },
                { "sort", _sort, _sort != null },
                { "fields", _projection, _projection != null },
                { "upsert", true, _isUpsert },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "writeConcern", () => WriteConcern.ToBsonDocument(), WriteConcern != null && !WriteConcern.IsServerDefault && Feature.FindAndModifyWriteConcern.IsSupported(serverVersion) },
                { "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue&& Feature.BypassDocumentValidation.IsSupported(serverVersion) },
                { "collation", () => Collation.ToBsonDocument(), Collation != null },
                { "txnNumber", () => transactionNumber, transactionNumber.HasValue }
            });
        }
        // methods
        internal BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
        {
            Feature.ReadConcern.ThrowIfNotSupported(connectionDescription.ServerVersion, _readConcern);
            Feature.Collation.ThrowIfNotSupported(connectionDescription.ServerVersion, _collation);

            var command = new BsonDocument
            {
                { "count", _collectionNamespace.CollectionName },
                { "query", _filter, _filter != null },
                { "limit", () => _limit.Value, _limit.HasValue },
                { "skip", () => _skip.Value, _skip.HasValue },
                { "hint", _hint, _hint != null },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null }
            };

            ReadConcernHelper.AppendReadConcern(command, _readConcern, connectionDescription, session);

            return(command);
        }
        internal BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
        {
            var readConcern = ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern);

            return(new BsonDocument
            {
                { "geoNear", _collectionNamespace.CollectionName },
                { "near", _near },
                { "limit", () => _limit.Value, _limit.HasValue },
                { "maxDistance", () => _maxDistance.Value, _maxDistance.HasValue },
                { "query", _filter, _filter != null },
                { "spherical", () => _spherical.Value, _spherical.HasValue },
                { "distanceMultiplier", () => _distanceMultiplier.Value, _distanceMultiplier.HasValue },
                { "includeLocs", () => _includeLocs.Value, _includeLocs.HasValue },
                { "uniqueDocs", () => _uniqueDocs.Value, _uniqueDocs.HasValue },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "readConcern", readConcern, readConcern != null }
            });
        }
        // methods
        internal override BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.Collation.ThrowIfNotSupported(serverVersion, Collation);

            return(new BsonDocument
            {
                { "findAndModify", CollectionNamespace.CollectionName },
                { "query", _filter },
                { "update", _update },
                { "new", true, _returnDocument == ReturnDocument.After },
                { "sort", _sort, _sort != null },
                { "fields", _projection, _projection != null },
                { "upsert", true, _isUpsert },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "writeConcern", () => WriteConcern.ToBsonDocument(), WriteConcern != null && !WriteConcern.IsServerDefault && Feature.FindAndModifyWriteConcern.IsSupported(serverVersion) },
                { "bypassDocumentValidation", () => _bypassDocumentValidation.Value, _bypassDocumentValidation.HasValue&& Feature.BypassDocumentValidation.IsSupported(serverVersion) },
                { "collation", () => Collation.ToBsonDocument(), Collation != null },
                { "arrayFilters", () => new BsonArray(_arrayFilters), _arrayFilters != null }
            });
        }
        internal BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.ReadConcern.ThrowIfNotSupported(serverVersion, _readConcern);
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            return(new BsonDocument
            {
                { "geoNear", _collectionNamespace.CollectionName },
                { "near", _near },
                { "limit", () => _limit.Value, _limit.HasValue },
                { "maxDistance", () => _maxDistance.Value, _maxDistance.HasValue },
                { "query", _filter, _filter != null },
                { "spherical", () => _spherical.Value, _spherical.HasValue },
                { "distanceMultiplier", () => _distanceMultiplier.Value, _distanceMultiplier.HasValue },
                { "includeLocs", () => _includeLocs.Value, _includeLocs.HasValue },
                { "uniqueDocs", () => _uniqueDocs.Value, _uniqueDocs.HasValue },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "readConcern", _readConcern.ToBsonDocument(), !_readConcern.IsServerDefault },
                { "collation", () => _collation.ToBsonDocument(), _collation != null }
            });
        }
        // methods
        internal override BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription, long?transactionNumber)
        {
            var serverVersion = connectionDescription.ServerVersion;

            Feature.Collation.ThrowIfNotSupported(serverVersion, Collation);

            var writeConcern = WriteConcernHelper.GetWriteConcernForCommand(session, WriteConcern, serverVersion, Feature.FindAndModifyWriteConcern);

            return(new BsonDocument
            {
                { "findAndModify", CollectionNamespace.CollectionName },
                { "query", _filter },
                { "remove", true },
                { "sort", _sort, _sort != null },
                { "fields", _projection, _projection != null },
                { "maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "writeConcern", writeConcern, writeConcern != null },
                { "collation", () => Collation.ToBsonDocument(), Collation != null },
                { "txnNumber", () => transactionNumber, transactionNumber.HasValue }
            });
        }
        // methods
        internal BsonDocument CreateCommand(SemanticVersion serverVersion)
        {
            Feature.Collation.ThrowIfNotSupported(serverVersion, _collation);

            return(new BsonDocument
            {
                { "create", _collectionNamespace.CollectionName },
                { "capped", () => _capped.Value, _capped.HasValue },
                { "autoIndexId", () => _autoIndexId.Value, _autoIndexId.HasValue },
                { "size", () => _maxSize.Value, _maxSize.HasValue },
                { "max", () => _maxDocuments.Value, _maxDocuments.HasValue },
                { "flags", () => _usePowerOf2Sizes.Value ? 1 : 0, _usePowerOf2Sizes.HasValue },
                { "storageEngine", () => _storageEngine, _storageEngine != null },
                { "indexOptionDefaults", _indexOptionDefaults, _indexOptionDefaults != null },
                { "validator", _validator, _validator != null },
                { "validationAction", () => _validationAction.Value.ToString().ToLowerInvariant(), _validationAction.HasValue },
                { "validationLevel", () => _validationLevel.Value.ToString().ToLowerInvariant(), _validationLevel.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null },
                { "writeConcern", () => _writeConcern.ToBsonDocument(), Feature.CommandsThatWriteAcceptWriteConcern.ShouldSendWriteConcern(serverVersion, _writeConcern) }
            });
        }
        // methods
        /// <summary>
        /// Creates the command.
        /// </summary>
        /// <param name="connectionDescription">The connection description.</param>
        /// <param name="session">The session.</param>
        /// <returns>The command.</returns>
        protected internal virtual BsonDocument CreateCommand(ConnectionDescription connectionDescription, ICoreSession session)
        {
            Feature.Collation.ThrowIfNotSupported(connectionDescription.ServerVersion, _collation);

            return(new BsonDocument
            {
                { "mapreduce", _collectionNamespace.CollectionName }, // all lowercase command name for backwards compatibility
                { "map", _mapFunction },
                { "reduce", _reduceFunction },
                { "out", CreateOutputOptions() },
                { "query", _filter, _filter != null },
                { "sort", _sort, _sort != null },
                { "limit", () => _limit.Value, _limit.HasValue },
                { "finalize", _finalizeFunction, _finalizeFunction != null },
                { "scope", _scope, _scope != null },
                { "jsMode", () => _javaScriptMode.Value, _javaScriptMode.HasValue },
                { "verbose", () => _verbose.Value, _verbose.HasValue },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "collation", () => _collation.ToBsonDocument(), _collation != null }
            });
        }
        public void ToBsonDocument_should_return_expected_result_when_backwards_is_set(
            [Values("en_US", "fr_CA")]
            string locale,
            [Values(null, false, true)]
            bool? backwards)
        {
            var subject = new Collation(locale, backwards: backwards);

            var result = subject.ToBsonDocument();
            var json = subject.ToString();

            var expectedResult = new BsonDocument
            {
                { "locale", locale },
                { "backwards", () => backwards.Value, backwards.HasValue }
            };
            result.Should().Be(expectedResult);
            json.Should().Be(expectedResult.ToJson());
        }
        public void ToBsonDocument_should_return_expected_result_when_maxVariable_is_set(
            [Values("en_US", "fr_CA")]
            string locale,
            [Values(null, CollationMaxVariable.Punctuation, CollationMaxVariable.Space)]
            CollationMaxVariable? maxVariable)
        {
            var subject = new Collation(locale, maxVariable: maxVariable);

            var result = subject.ToBsonDocument();
            var json = subject.ToString();

            var expectedResult = new BsonDocument
            {
                { "locale", locale },
                { "maxVariable", () => Collation.ToString(maxVariable.Value), maxVariable.HasValue }
            };
            result.Should().Be(expectedResult);
            json.Should().Be(expectedResult.ToJson());
        }
        public void CreateCommand_should_return_expected_result_when_Collation_is_set(
            [Values("en_US", "fr_CA")]
            string locale)
        {
            var collation = new Collation(locale);
            var subject = new AggregateExplainOperation(_collectionNamespace, __pipeline, _messageEncoderSettings)
            {
                Collation = collation
            };

            var result = subject.CreateCommand(Feature.Collation.FirstSupportedVersion);

            var expectedResult = new BsonDocument
            {
                { "aggregate", _collectionNamespace.CollectionName },
                { "explain", true },
                { "pipeline", new BsonArray(__pipeline) },
                { "collation", collation.ToBsonDocument() }
            };
            result.Should().Be(expectedResult);
        }
        public void ToBsonDocument_should_return_expected_result_when_caseLevel_is_set(
            [Values("en_US", "fr_CA")]
            string locale,
            [Values(null, false, true)]
            bool? caseLevel)
        {
            var subject = new Collation(locale, caseLevel: caseLevel);

            var result = subject.ToBsonDocument();
            var json = subject.ToString();

            var expectedResult = new BsonDocument
            {
                { "locale", locale },
                { "caseLevel", () => caseLevel.Value, caseLevel.HasValue }
            };
            result.Should().Be(expectedResult);
            json.Should().Be(expectedResult.ToJson());
        }
        public void ToBsonDocument_should_return_expected_result_when_caseFirst_is_set(
            [Values("en_US", "fr_CA")]
            string locale,
            [Values(null, CollationCaseFirst.Lower, CollationCaseFirst.Upper)]
            CollationCaseFirst? caseFirst)
        {
            var subject = new Collation(locale, caseFirst: caseFirst);

            var result = subject.ToBsonDocument();
            var json = subject.ToString();

            var expectedResult = new BsonDocument
            {
                { "locale", locale },
                { "caseFirst", () => Collation.ToString(caseFirst.Value), caseFirst.HasValue }
            };
            result.Should().Be(expectedResult);
            json.Should().Be(expectedResult.ToJson());
        }
        public void ToBsonDocument_should_return_expected_result(
            [Values("en_US", "fr_CA")]
            string locale)
        {
            var subject = new Collation(locale);

            var result = subject.ToBsonDocument();
            var json = subject.ToString();

            var expectedResult = new BsonDocument("locale", locale);
            result.Should().Be(expectedResult);
            json.Should().Be(expectedResult.ToJson());
        }
        public void ToBsonDocument_should_return_expected_result_when_numericOrdering_is_set(
            [Values("en_US", "fr_CA")]
            string locale,
            [Values(null, false, true)]
            bool? numericOrdering)
        {
            var subject = new Collation(locale, numericOrdering: numericOrdering);

            var result = subject.ToBsonDocument();
            var json = subject.ToString();

            var expectedResult = new BsonDocument
            {
                { "locale", locale },
                { "numericOrdering", () => numericOrdering.Value, numericOrdering.HasValue }
            };
            result.Should().Be(expectedResult);
            json.Should().Be(expectedResult.ToJson());
        }
        public void ToBsonDocument_should_return_expected_result_when_strength_is_set(
            [Values("en_US", "fr_CA")]
            string locale,
            [Values(null, CollationStrength.Primary, CollationStrength.Identical)]
            CollationStrength? strength)
        {
            var subject = new Collation(locale, strength: strength);

            var result = subject.ToBsonDocument();
            var json = subject.ToString();

            var expectedResult = new BsonDocument
            {
                { "locale", locale },
                { "strength", () => Collation.ToInt32(strength.Value), strength.HasValue }
            };
            result.Should().Be(expectedResult);
            json.Should().Be(expectedResult.ToJson());
        }
        public void ToBsonDocument_should_return_expected_result_when_alternate_is_set(
            [Values("en_US", "fr_CA")]
            string locale,
            [Values(null, CollationAlternate.NonIgnorable, CollationAlternate.Shifted)]
            CollationAlternate? alternate)
        {
            var subject = new Collation(locale, alternate: alternate);

            var result = subject.ToBsonDocument();
            var json = subject.ToString();

            var expectedResult = new BsonDocument
            {
                { "locale", locale },
                { "alternate", () => Collation.ToString(alternate.Value), alternate.HasValue }
            };
            result.Should().Be(expectedResult);
            json.Should().Be(expectedResult.ToJson());
        }