Skip to content

Commit 2029632

Browse files
committed
CSHARP-1427: Added support for BypassDocumentValidation.
1 parent f20f371 commit 2029632

19 files changed

+192
-46
lines changed

MongoDB.Driver/AggregateArgs.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class AggregateArgs
4343
// private fields
4444
private bool? _allowDiskUse;
4545
private int? _batchSize;
46+
private bool? _bypassDocumentValidation;
4647
private TimeSpan? _maxTime;
4748
private AggregateOutputMode _outputMode = AggregateOutputMode.Inline;
4849
private IEnumerable<BsonDocument> _pipeline;
@@ -80,6 +81,18 @@ public int? BatchSize
8081
}
8182
}
8283

84+
/// <summary>
85+
/// Gets or sets a value indicating whether to bypass document validation.
86+
/// </summary>
87+
/// <value>
88+
/// A value indicating whether to bypass document validation.
89+
/// </value>
90+
public bool? BypassDocumentValidation
91+
{
92+
get { return _bypassDocumentValidation; }
93+
set { _bypassDocumentValidation = value; }
94+
}
95+
8396
/// <summary>
8497
/// Gets or sets the max time the server should spend on the aggregation command.
8598
/// </summary>

MongoDB.Driver/Communication/FeatureDetection/FeatureSetDetector.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ internal class FeatureSetDetector
6060

6161
// added in 3.2.0
6262
new FeatureSetDependency(
63-
new ServerVersionDependency(3, 1, 9999),
63+
new ServerVersionDependency(3, 2, 0),
64+
FeatureId.BypassDocumentValidation,
6465
FeatureId.FindAndModifyWriteConcern)
6566
};
6667

MongoDB.Driver/FeatureId.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public enum FeatureId
3737
/// </summary>
3838
AggregateOutputToCollection,
3939
/// <summary>
40+
/// The bypass document validation feature.
41+
/// </summary>
42+
BypassDocumentValidation,
43+
/// <summary>
4044
/// The create index command feature.
4145
/// </summary>
4246
CreateIndexCommand,

MongoDB.Driver/FindAndModifyArgs.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public enum FindAndModifyDocumentVersion
3838
public class FindAndModifyArgs
3939
{
4040
// private fields
41+
private bool? _bypassDocumentValidation;
4142
private IMongoFields _fields;
4243
private TimeSpan? _maxTime;
4344
private IMongoQuery _query;
@@ -47,6 +48,18 @@ public class FindAndModifyArgs
4748
private FindAndModifyDocumentVersion? _versionReturned;
4849

4950
// public properties
51+
/// <summary>
52+
/// Gets or sets a value indicating whether to bypass document validation.
53+
/// </summary>
54+
/// <value>
55+
/// A value indicating whether to bypass document validation.
56+
/// </value>
57+
public bool? BypassDocumentValidation
58+
{
59+
get { return _bypassDocumentValidation; }
60+
set { _bypassDocumentValidation = value; }
61+
}
62+
5063
/// <summary>
5164
/// Gets or sets the fields specification.
5265
/// </summary>

MongoDB.Driver/MapReduceArgs.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public enum MapReduceOutputMode
4747
public class MapReduceArgs
4848
{
4949
// private fields
50+
private bool? _bypassDocumentValidation;
5051
private BsonJavaScript _finalizeFunction;
5152
private bool? _jsMode;
5253
private long? _limit;
@@ -64,6 +65,18 @@ public class MapReduceArgs
6465
private bool? _verbose;
6566

6667
// public properties
68+
/// <summary>
69+
/// Gets or sets a value indicating whether to bypass document validation.
70+
/// </summary>
71+
/// <value>
72+
/// A value indicating whether to bypass document validation.
73+
/// </value>
74+
public bool? BypassDocumentValidation
75+
{
76+
get { return _bypassDocumentValidation; }
77+
set { _bypassDocumentValidation = value; }
78+
}
79+
6780
/// <summary>
6881
/// Gets or sets the finalize function.
6982
/// </summary>

MongoDB.Driver/MongoCollection.cs

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ internal virtual BulkWriteResult BulkWrite(BulkWriteArgs args)
220220
maxDocumentSize,
221221
maxWireDocumentSize,
222222
args.IsOrdered ?? true,
223+
args.BypassDocumentValidation,
223224
GetBinaryReaderSettings(),
224225
args.Requests,
225226
writeConcern,
@@ -655,7 +656,8 @@ public virtual FindAndModifyResult FindAndModify(FindAndModifyArgs args)
655656
{ "fields", () => BsonDocumentWrapper.Create(args.Fields), args.Fields != null }, // optional
656657
{ "upsert", true, args.Upsert}, // optional
657658
{ "maxTimeMS", () => args.MaxTime.Value.TotalMilliseconds, args.MaxTime.HasValue }, // optional
658-
{ "writeConcern", writeConcern, writeConcern != null && serverInstance.Supports(FeatureId.FindAndModifyWriteConcern) }
659+
{ "writeConcern", writeConcern, writeConcern != null && serverInstance.Supports(FeatureId.FindAndModifyWriteConcern) },
660+
{ "bypassDocumentValidation", () => args.BypassDocumentValidation.Value, args.BypassDocumentValidation.HasValue && serverInstance.Supports(FeatureId.BypassDocumentValidation) }
659661
};
660662
try
661663
{
@@ -1603,6 +1605,7 @@ public virtual IEnumerable<WriteConcernResult> InsertBatch(
16031605
maxBatchCount,
16041606
maxBatchLength,
16051607
isOrdered,
1608+
options.BypassDocumentValidation,
16061609
GetBinaryReaderSettings(),
16071610
requests,
16081611
writeConcern,
@@ -1703,43 +1706,49 @@ public virtual MapReduceResult MapReduce(MapReduceArgs args)
17031706
if (args.MapFunction == null) { throw new ArgumentException("MapFunction is null.", "args"); }
17041707
if (args.ReduceFunction == null) { throw new ArgumentException("ReduceFunction is null.", "args"); }
17051708

1706-
BsonDocument output;
1707-
if (args.OutputMode == MapReduceOutputMode.Inline)
1708-
{
1709-
output = new BsonDocument("inline", 1);
1710-
}
1711-
else
1709+
using (var request = _server.RequestStart(null))
17121710
{
1713-
if (args.OutputCollectionName == null) { throw new ArgumentException("OutputCollectionName is null and OutputMode is not Inline.", "args"); }
1714-
var action = MongoUtils.ToCamelCase(args.OutputMode.ToString());
1715-
output = new BsonDocument
1711+
var serverInstance = _server.RequestConnection.ServerInstance;
1712+
1713+
BsonDocument output;
1714+
if (args.OutputMode == MapReduceOutputMode.Inline)
1715+
{
1716+
output = new BsonDocument("inline", 1);
1717+
}
1718+
else
1719+
{
1720+
if (args.OutputCollectionName == null) { throw new ArgumentException("OutputCollectionName is null and OutputMode is not Inline.", "args"); }
1721+
var action = MongoUtils.ToCamelCase(args.OutputMode.ToString());
1722+
output = new BsonDocument
17161723
{
17171724
{ action, args.OutputCollectionName },
17181725
{ "db", args.OutputDatabaseName, args.OutputDatabaseName != null }, // optional
17191726
{ "sharded", () => args.OutputIsSharded.Value, args.OutputIsSharded.HasValue }, // optional
17201727
{ "nonAtomic", () => args.OutputIsNonAtomic.Value, args.OutputIsNonAtomic.HasValue } // optional
17211728
};
1722-
}
1729+
}
17231730

1724-
var command = new CommandDocument
1725-
{
1726-
{ "mapreduce", _name }, // all lowercase for backwards compatibility
1727-
{ "map", args.MapFunction },
1728-
{ "reduce", args.ReduceFunction },
1729-
{ "out", output },
1730-
{ "query", () => BsonDocumentWrapper.Create(args.Query), args.Query != null }, // optional
1731-
{ "sort", () => BsonDocumentWrapper.Create(args.SortBy), args.SortBy != null }, // optional
1732-
{ "limit", () => args.Limit.Value, args.Limit.HasValue }, // optional
1733-
{ "finalize", args.FinalizeFunction, args.FinalizeFunction != null }, // optional
1734-
{ "scope", () => BsonDocumentWrapper.Create(args.Scope), args.Scope != null }, // optional
1735-
{ "jsMode", () => args.JsMode.Value, args.JsMode.HasValue }, // optional
1736-
{ "verbose", () => args.Verbose.Value, args.Verbose.HasValue }, // optional
1737-
{ "maxTimeMS", () => args.MaxTime.Value.TotalMilliseconds, args.MaxTime.HasValue } // optional
1738-
};
1739-
var result = RunCommandAs<MapReduceResult>(command);
1740-
result.SetInputDatabase(_database);
1731+
var command = new CommandDocument
1732+
{
1733+
{ "mapreduce", _name }, // all lowercase for backwards compatibility
1734+
{ "map", args.MapFunction },
1735+
{ "reduce", args.ReduceFunction },
1736+
{ "out", output },
1737+
{ "query", () => BsonDocumentWrapper.Create(args.Query), args.Query != null }, // optional
1738+
{ "sort", () => BsonDocumentWrapper.Create(args.SortBy), args.SortBy != null }, // optional
1739+
{ "limit", () => args.Limit.Value, args.Limit.HasValue }, // optional
1740+
{ "finalize", args.FinalizeFunction, args.FinalizeFunction != null }, // optional
1741+
{ "scope", () => BsonDocumentWrapper.Create(args.Scope), args.Scope != null }, // optional
1742+
{ "jsMode", () => args.JsMode.Value, args.JsMode.HasValue }, // optional
1743+
{ "verbose", () => args.Verbose.Value, args.Verbose.HasValue }, // optional
1744+
{ "maxTimeMS", () => args.MaxTime.Value.TotalMilliseconds, args.MaxTime.HasValue }, // optional
1745+
{ "bypassDocumentValidation", () => args.BypassDocumentValidation.Value, args.BypassDocumentValidation.HasValue && serverInstance.Supports(FeatureId.BypassDocumentValidation) }
1746+
};
1747+
var result = RunCommandAs<MapReduceResult>(command);
1748+
result.SetInputDatabase(_database);
17411749

1742-
return result;
1750+
return result;
1751+
}
17431752
}
17441753

17451754
/// <summary>
@@ -2101,6 +2110,7 @@ public virtual WriteConcernResult Update(IMongoQuery query, IMongoUpdate update,
21012110
maxBatchCount,
21022111
maxBatchLength,
21032112
isOrdered,
2113+
options.BypassDocumentValidation,
21042114
GetBinaryReaderSettings(),
21052115
requests,
21062116
writeConcern,
@@ -2216,25 +2226,31 @@ internal BsonBinaryWriterSettings GetWriterSettings(MongoConnection connection)
22162226

22172227
internal AggregateResult RunAggregateCommand(AggregateArgs args)
22182228
{
2219-
BsonDocument cursor = null;
2220-
if (args.OutputMode == AggregateOutputMode.Cursor)
2229+
using (var request = _server.RequestStart(null))
22212230
{
2222-
cursor = new BsonDocument
2231+
var serverInstance = _server.RequestConnection.ServerInstance;
2232+
2233+
BsonDocument cursor = null;
2234+
if (args.OutputMode == AggregateOutputMode.Cursor)
2235+
{
2236+
cursor = new BsonDocument
22232237
{
22242238
{ "batchSize", () => args.BatchSize.Value, args.BatchSize.HasValue }
22252239
};
2226-
}
2240+
}
22272241

2228-
var aggregateCommand = new CommandDocument
2229-
{
2230-
{ "aggregate", _name },
2231-
{ "pipeline", new BsonArray(args.Pipeline.Cast<BsonValue>()) },
2232-
{ "cursor", cursor, cursor != null }, // optional
2233-
{ "allowDiskUse", () => args.AllowDiskUse.Value, args.AllowDiskUse.HasValue }, // optional
2234-
{ "maxTimeMS", () => args.MaxTime.Value.TotalMilliseconds, args.MaxTime.HasValue } // optional
2235-
};
2242+
var aggregateCommand = new CommandDocument
2243+
{
2244+
{ "aggregate", _name },
2245+
{ "pipeline", new BsonArray(args.Pipeline.Cast<BsonValue>()) },
2246+
{ "cursor", cursor, cursor != null }, // optional
2247+
{ "allowDiskUse", () => args.AllowDiskUse.Value, args.AllowDiskUse.HasValue }, // optional
2248+
{ "maxTimeMS", () => args.MaxTime.Value.TotalMilliseconds, args.MaxTime.HasValue }, // optional
2249+
{ "bypassDocumentValidation", () => args.BypassDocumentValidation.Value, args.BypassDocumentValidation.HasValue && serverInstance.Supports(FeatureId.BypassDocumentValidation) }
2250+
};
22362251

2237-
return RunCommandAs<AggregateResult>(aggregateCommand);
2252+
return RunCommandAs<AggregateResult>(aggregateCommand);
2253+
}
22382254
}
22392255

22402256
// private methods

MongoDB.Driver/MongoInsertOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace MongoDB.Driver
2323
public class MongoInsertOptions
2424
{
2525
// private fields
26+
private bool? _bypassDocumentValidation;
2627
private bool _checkElementNames;
2728
private InsertFlags _flags;
2829
private WriteConcern _writeConcern;
@@ -48,6 +49,18 @@ public MongoInsertOptions(MongoCollection collection) : this()
4849
}
4950

5051
// public properties
52+
/// <summary>
53+
/// Gets or sets a value indicating whether to bypass document validation.
54+
/// </summary>
55+
/// <value>
56+
/// A value indicating whether to bypass document validation.
57+
/// </value>
58+
public bool? BypassDocumentValidation
59+
{
60+
get { return _bypassDocumentValidation; }
61+
set { _bypassDocumentValidation = value; }
62+
}
63+
5164
/// <summary>
5265
/// Gets or sets whether to check element names before proceeding with the Insert.
5366
/// </summary>

MongoDB.Driver/MongoUpdateOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace MongoDB.Driver
2323
public class MongoUpdateOptions
2424
{
2525
// private fields
26+
private bool? _bypassDocumentValidation;
2627
private bool _checkElementNames;
2728
private UpdateFlags _flags;
2829
private WriteConcern _writeConcern;
@@ -48,6 +49,18 @@ public MongoUpdateOptions(MongoCollection collection) : this()
4849
}
4950

5051
// public properties
52+
/// <summary>
53+
/// Gets or sets a value indicating whether to bypass document validation.
54+
/// </summary>
55+
/// <value>
56+
/// A value indicating whether to bypass document validation.
57+
/// </value>
58+
public bool? BypassDocumentValidation
59+
{
60+
get { return _bypassDocumentValidation; }
61+
set { _bypassDocumentValidation = value; }
62+
}
63+
5164
/// <summary>
5265
/// Gets or sets whether to check element names before proceeding with the Update.
5366
/// </summary>

MongoDB.Driver/Operations/BulkDeleteOperationArgs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public BulkDeleteOperationArgs(
4141
maxBatchCount,
4242
maxBatchLength,
4343
isOrdered,
44+
null, // bypassDocumentValidation
4445
readerSettings,
4546
requests.Cast<WriteRequest>(),
4647
writeConcern,

MongoDB.Driver/Operations/BulkInsertOperationArgs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public BulkInsertOperationArgs(
3939
int maxBatchCount,
4040
int maxBatchLength,
4141
bool isOrdered,
42+
bool? bypassDocumentValidation,
4243
BsonBinaryReaderSettings readerSettings,
4344
IEnumerable<InsertRequest> requests,
4445
WriteConcern writeConcern,
@@ -49,6 +50,7 @@ public BulkInsertOperationArgs(
4950
maxBatchCount,
5051
maxBatchLength,
5152
isOrdered,
53+
bypassDocumentValidation,
5254
readerSettings,
5355
requests.Cast<WriteRequest>(),
5456
writeConcern,

0 commit comments

Comments
 (0)