Skip to content

Commit eb2c73d

Browse files
CSHARP-1896: Support deprecated modifiers FindOption in CRUD API
1 parent 5e33c62 commit eb2c73d

File tree

12 files changed

+196
-8
lines changed

12 files changed

+196
-8
lines changed

src/MongoDB.Driver.Core/Core/Operations/FindOperation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ public BsonDocument Min
277277
/// <value>
278278
/// The additional query modifiers.
279279
/// </value>
280+
[Obsolete("Use individual properties instead.")]
280281
public BsonDocument Modifiers
281282
{
282283
get { return _modifiers; }

src/MongoDB.Driver.Legacy/MongoCursor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,9 @@ private IEnumerator<TDocument> GetEnumerator(IClientSessionHandle session)
869869
Filter = queryDocument,
870870
Limit = Limit,
871871
MaxAwaitTime = MaxAwaitTime,
872+
#pragma warning disable 618
872873
Modifiers = Options,
874+
#pragma warning restore 618
873875
NoCursorTimeout = noCursorTimeout,
874876
Projection = Fields.ToBsonDocument(),
875877
ReadConcern = ReadConcern,

src/MongoDB.Driver/FindFluent.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,20 @@ public override IFindFluent<TDocument, TNewProjection> Project<TNewProjection>(P
128128
Collation = _options.Collation,
129129
Comment = _options.Comment,
130130
CursorType = _options.CursorType,
131+
Hint = _options.Hint,
131132
Limit = _options.Limit,
133+
Max = _options.Max,
132134
MaxAwaitTime = _options.MaxAwaitTime,
133135
MaxTime = _options.MaxTime,
136+
Min = _options.Min,
137+
#pragma warning disable 618
134138
Modifiers = _options.Modifiers,
139+
#pragma warning restore 618
135140
NoCursorTimeout = _options.NoCursorTimeout,
136141
OplogReplay = _options.OplogReplay,
137142
Projection = projection,
143+
ReturnKey = _options.ReturnKey,
144+
ShowRecordId = _options.ShowRecordId,
138145
Skip = _options.Skip,
139146
Sort = _options.Sort,
140147
};
@@ -219,14 +226,41 @@ public override string ToString()
219226
sb.Append(".maxTime(" + _options.MaxTime.Value.TotalMilliseconds + ")");
220227
}
221228

229+
if (_options.Hint != null)
230+
{
231+
sb.Append(".hint(" + _options.Hint + ")");
232+
}
233+
234+
if (_options.Max != null)
235+
{
236+
sb.Append(".max(" + _options.Max + ")");
237+
}
238+
239+
if (_options.Min != null)
240+
{
241+
sb.Append(".min(" + _options.Min + ")");
242+
}
243+
244+
if (_options.ReturnKey.HasValue)
245+
{
246+
sb.Append(".returnKey(" + _options.ReturnKey.Value.ToString().ToLower() + ")");
247+
}
248+
249+
if (_options.ShowRecordId.HasValue)
250+
{
251+
sb.Append(".showRecordId(" + _options.ShowRecordId.Value.ToString().ToLower() + ")");
252+
}
253+
222254
if (_options.Comment != null)
223255
{
224256
sb.Append("._addSpecial(\"$comment\", \"" + _options.Comment + "\")");
225257
}
226258

259+
#pragma warning disable 618
227260
if (_options.Modifiers != null)
228261
{
229262
foreach (var modifier in _options.Modifiers)
263+
#pragma warning restore 618
230264
{
231265
sb.Append("._addSpecial(\"" + modifier.Name + "\", ");
232266
if (modifier.Value.BsonType == BsonType.String)
@@ -250,7 +284,9 @@ private CountOptions CreateCountOptions()
250284
return new CountOptions
251285
{
252286
Collation = _options.Collation,
253-
Hint = _options.Modifiers?.GetValue("$hint", null),
287+
#pragma warning disable 618
288+
Hint = _options.Hint ?? _options.Modifiers?.GetValue("$hint", null),
289+
#pragma warning restore 618
254290
Limit = _options.Limit,
255291
MaxTime = _options.MaxTime,
256292
Skip = _options.Skip

src/MongoDB.Driver/FindOptions.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ public abstract class FindOptionsBase
3030
private Collation _collation;
3131
private string _comment;
3232
private CursorType _cursorType;
33+
private BsonValue _hint;
34+
private BsonDocument _max;
3335
private TimeSpan? _maxAwaitTime;
3436
private TimeSpan? _maxTime;
37+
private BsonDocument _min;
3538
private BsonDocument _modifiers;
3639
private bool? _noCursorTimeout;
3740
private bool? _oplogReplay;
41+
private bool? _returnKey;
42+
private bool? _showRecordId;
3843

3944
// constructors
4045
/// <summary>
@@ -91,6 +96,24 @@ public CursorType CursorType
9196
set { _cursorType = value; }
9297
}
9398

99+
/// <summary>
100+
/// Gets or sets the hint.
101+
/// </summary>
102+
public BsonValue Hint
103+
{
104+
get { return _hint; }
105+
set { _hint = value; }
106+
}
107+
108+
/// <summary>
109+
/// Gets or sets the max key value.
110+
/// </summary>
111+
public BsonDocument Max
112+
{
113+
get { return _max; }
114+
set { _max = value; }
115+
}
116+
94117
/// <summary>
95118
/// Gets or sets the maximum await time for TailableAwait cursors.
96119
/// </summary>
@@ -109,9 +132,19 @@ public TimeSpan? MaxTime
109132
set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
110133
}
111134

135+
/// <summary>
136+
/// Gets or sets the min key value.
137+
/// </summary>
138+
public BsonDocument Min
139+
{
140+
get { return _min; }
141+
set { _min = value; }
142+
}
143+
112144
/// <summary>
113145
/// Gets or sets the modifiers.
114146
/// </summary>
147+
[Obsolete("Use individual properties instead.")]
115148
public BsonDocument Modifiers
116149
{
117150
get { return _modifiers; }
@@ -135,6 +168,24 @@ public bool? OplogReplay
135168
get { return _oplogReplay; }
136169
set { _oplogReplay = value; }
137170
}
171+
172+
/// <summary>
173+
/// Gets or sets returnKey. If true, returns only the index keys in the resulting documents.
174+
/// </summary>
175+
public bool? ReturnKey
176+
{
177+
get { return _returnKey; }
178+
set { _returnKey = value; }
179+
}
180+
181+
/// <summary>
182+
/// Gets or sets whether the record Id should be added to the result document.
183+
/// </summary>
184+
public bool? ShowRecordId
185+
{
186+
get { return _showRecordId; }
187+
set { _showRecordId = value; }
188+
}
138189
}
139190

140191
/// <summary>

src/MongoDB.Driver/IMongoCollectionExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,18 @@ private static IFindFluent<TDocument, TDocument> FindHelper<TDocument>(IClientSe
839839
Collation = options.Collation,
840840
Comment = options.Comment,
841841
CursorType = options.CursorType,
842+
Hint = options.Hint,
843+
Max = options.Max,
842844
MaxAwaitTime = options.MaxAwaitTime,
843845
MaxTime = options.MaxTime,
846+
Min = options.Min,
847+
#pragma warning disable 618
844848
Modifiers = options.Modifiers,
849+
#pragma warning restore 618
845850
NoCursorTimeout = options.NoCursorTimeout,
846-
OplogReplay = options.OplogReplay
851+
OplogReplay = options.OplogReplay,
852+
ReturnKey = options.ReturnKey,
853+
ShowRecordId = options.ShowRecordId,
847854
};
848855
}
849856

src/MongoDB.Driver/MongoCollectionImpl.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,15 +967,22 @@ private FindOperation<TProjection> CreateFindOperation<TProjection>(FilterDefini
967967
Comment = options.Comment,
968968
CursorType = options.CursorType.ToCore(),
969969
Filter = filter.Render(_documentSerializer, _settings.SerializerRegistry),
970+
Hint = options.Hint,
970971
Limit = options.Limit,
972+
Max = options.Max,
971973
MaxAwaitTime = options.MaxAwaitTime,
972974
MaxTime = options.MaxTime,
975+
Min = options.Min,
976+
#pragma warning disable 618
973977
Modifiers = options.Modifiers,
978+
#pragma warning restore 618
974979
NoCursorTimeout = options.NoCursorTimeout,
975980
OplogReplay = options.OplogReplay,
976981
Projection = renderedProjection.Document,
977982
ReadConcern = _settings.ReadConcern,
978983
RetryRequested = _database.Client.Settings.RetryReads,
984+
ReturnKey = options.ReturnKey,
985+
ShowRecordId = options.ShowRecordId,
979986
Skip = options.Skip,
980987
Sort = options.Sort == null ? null : options.Sort.Render(_documentSerializer, _settings.SerializerRegistry)
981988
};

tests/MongoDB.Driver.Core.Tests/Core/Operations/FindOperationTests.cs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ public void constructor_should_initialize_instance()
143143
#pragma warning restore
144144
subject.MaxTime.Should().NotHaveValue();
145145
subject.Min.Should().BeNull();
146+
#pragma warning disable 618
146147
subject.Modifiers.Should().BeNull();
148+
#pragma warning restore 618
147149
subject.NoCursorTimeout.Should().NotHaveValue();
148150
subject.OplogReplay.Should().NotHaveValue();
149151
subject.Projection.Should().BeNull();
@@ -186,6 +188,51 @@ public void constructor_should_throw_when_resultSerializer_is_null()
186188
argumentNullException.ParamName.Should().Be("resultSerializer");
187189
}
188190

191+
[Fact]
192+
public void CreateFindCommandOperation_should_prefer_top_level_fields_over_modifiers()
193+
{
194+
var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
195+
{
196+
Hint = "x_2",
197+
Max = new BsonDocument("max", 5),
198+
#pragma warning disable 618
199+
MaxScan = 7,
200+
#pragma warning restore 618
201+
Min = new BsonDocument("min", 3),
202+
ReturnKey = true,
203+
ShowRecordId = true,
204+
#pragma warning disable 618
205+
Snapshot = true,
206+
#pragma warning restore 618
207+
#pragma warning disable 618
208+
Modifiers = new BsonDocument
209+
{
210+
{ "$hint", "x_1" },
211+
{ "$max", new BsonDocument("max", 1) },
212+
{ "$maxScan", 1 },
213+
{ "$min", new BsonDocument("min", 1) },
214+
{ "$returnKey", false },
215+
{ "$showDiskLoc", false },
216+
{ "$snapshot", false }
217+
}
218+
#pragma warning restore 618
219+
};
220+
221+
var result = subject.CreateFindCommandOperation();
222+
223+
result.Hint.Should().Be(subject.Hint);
224+
result.Max.Should().Be(subject.Max);
225+
#pragma warning disable 618
226+
result.MaxScan.Should().Be(subject.MaxScan);
227+
#pragma warning restore 618
228+
result.Min.Should().Be(subject.Min);
229+
result.ReturnKey.Should().Be(subject.ReturnKey);
230+
result.ShowRecordId.Should().Be(subject.ShowRecordId);
231+
#pragma warning disable 618
232+
result.Snapshot.Should().Be(subject.Snapshot);
233+
#pragma warning restore 618
234+
}
235+
189236
[Fact]
190237
public void CreateFindCommandOperation_should_return_expected_result()
191238
{
@@ -261,6 +308,7 @@ public void CreateFindCommandOperation_should_return_expected_result_when_modifi
261308
{
262309
var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
263310
{
311+
#pragma warning disable 618
264312
Modifiers = new BsonDocument
265313
{
266314
{ "$comment", "comment" },
@@ -274,24 +322,23 @@ public void CreateFindCommandOperation_should_return_expected_result_when_modifi
274322
{ "$showDiskLoc", true },
275323
{ "$snapshot", true }
276324
}
325+
#pragma warning restore 618
277326
};
278327

279328
var result = subject.CreateFindCommandOperation();
280329

330+
#pragma warning disable 618
281331
result.Comment.Should().Be(subject.Modifiers["$comment"].AsString);
282332
result.Hint.Should().Be(subject.Modifiers["$hint"]);
283333
result.Max.Should().Be(subject.Modifiers["$max"].AsBsonDocument);
284-
#pragma warning disable 618
285334
result.MaxScan.Should().Be(subject.Modifiers["$maxScan"].AsInt32);
286-
#pragma warning restore
287335
result.MaxTime.Should().Be(TimeSpan.FromMilliseconds(subject.Modifiers["$maxTimeMS"].AsInt32));
288336
result.Min.Should().Be(subject.Modifiers["$min"].AsBsonDocument);
289337
result.ReturnKey.Should().Be(subject.Modifiers["$returnKey"].ToBoolean());
290338
result.ShowRecordId.Should().Be(subject.Modifiers["$showDiskLoc"].AsBoolean);
291-
#pragma warning disable 618
292339
result.Snapshot.Should().Be(subject.Modifiers["$snapshot"].AsBoolean);
293-
#pragma warning restore
294340
result.Sort.Should().Be(subject.Modifiers["$orderby"].AsBsonDocument);
341+
#pragma warning restore 618
295342
}
296343

297344
[Fact]
@@ -343,7 +390,9 @@ public void CreateFindOpcodeOperation_should_return_expected_result()
343390
result.MaxTime.Should().Be(subject.MaxTime);
344391
result.MessageEncoderSettings.Should().BeSameAs(subject.MessageEncoderSettings);
345392
result.Min.Should().Be(subject.Min);
393+
#pragma warning disable 618
346394
result.Modifiers.Should().Be(subject.Modifiers);
395+
#pragma warning restore 618
347396
result.NoCursorTimeout.Should().Be(subject.NoCursorTimeout);
348397
result.OplogReplay.Should().Be(subject.OplogReplay);
349398
result.Projection.Should().Be(subject.Projection);
@@ -804,9 +853,10 @@ public void Modifiers_get_and_set_should_work(
804853
{
805854
var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
806855
var value = valueString == null ? null : BsonDocument.Parse(valueString);
807-
856+
#pragma warning disable 618
808857
subject.Modifiers = value;
809858
var result = subject.Modifiers;
859+
#pragma warning restore 618
810860

811861
result.Should().Be(value);
812862
}

0 commit comments

Comments
 (0)