-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5672: Support sorting by value in PushEach operation #1748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9f29350
ea3784a
121021d
c0d08cf
533fe3e
e998575
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,15 @@ public static SortDefinition<TDocument> MetaTextScore<TDocument>(this SortDefini | |
/// <typeparam name="TDocument">The type of the document.</typeparam> | ||
public sealed class SortDefinitionBuilder<TDocument> | ||
{ | ||
/// <summary> | ||
/// Creates a value ascending sort. | ||
/// </summary> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering if should add a short example of what "value sort" is. As it might be not that evident to our users. |
||
/// <returns>A value ascending sort.</returns> | ||
public SortDefinition<TDocument> Ascending() | ||
{ | ||
return new ValueDirectionalSortDefinition<TDocument>(SortDirection.Ascending); | ||
} | ||
|
||
/// <summary> | ||
/// Creates an ascending sort. | ||
/// </summary> | ||
|
@@ -170,6 +179,15 @@ public SortDefinition<TDocument> Combine(IEnumerable<SortDefinition<TDocument>> | |
return new CombinedSortDefinition<TDocument>(sorts); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a value descending sort. | ||
/// </summary> | ||
/// <returns>A value descending sort.</returns> | ||
public SortDefinition<TDocument> Descending() | ||
{ | ||
return new ValueDirectionalSortDefinition<TDocument>(SortDirection.Descending); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a descending sort. | ||
/// </summary> | ||
|
@@ -232,6 +250,11 @@ internal sealed class CombinedSortDefinition<TDocument> : SortDefinition<TDocume | |
public CombinedSortDefinition(IEnumerable<SortDefinition<TDocument>> sorts) | ||
{ | ||
_sorts = Ensure.IsNotNull(sorts, nameof(sorts)).ToList(); | ||
|
||
if (_sorts.Any(sort => sort is ValueDirectionalSortDefinition<TDocument>)) | ||
{ | ||
throw new InvalidOperationException("Value-based sort cannot be combined with other sorts. When sorting by the entire element value, no other sorting criteria can be applied."); | ||
} | ||
} | ||
|
||
public override BsonDocument Render(RenderArgs<TDocument> args) | ||
|
@@ -272,20 +295,25 @@ public override BsonDocument Render(RenderArgs<TDocument> args) | |
{ | ||
var renderedField = _field.Render(args); | ||
|
||
BsonValue value; | ||
switch (_direction) | ||
{ | ||
case SortDirection.Ascending: | ||
value = 1; | ||
break; | ||
case SortDirection.Descending: | ||
value = -1; | ||
break; | ||
default: | ||
throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + "."); | ||
} | ||
return new BsonDocument(renderedField.FieldName, _direction.Render()); | ||
} | ||
} | ||
|
||
return new BsonDocument(renderedField.FieldName, value); | ||
internal sealed class ValueDirectionalSortDefinition<TDocument> : SortDefinition<TDocument> | ||
{ | ||
private readonly SortDirection _direction; | ||
|
||
public ValueDirectionalSortDefinition(SortDirection direction) | ||
{ | ||
_direction = direction; | ||
} | ||
|
||
public override BsonDocument Render(RenderArgs<TDocument> args) | ||
rstam marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, I'm using the exception thrown during rendering as the mechanism to inform users that value-based sorts aren't supported. Since I've only updated call sites where value-based sorts are explicitly supported, any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the way you have it now is fine. We can address whatever changes (if any) are needed later. Most places that currently call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @rstam, we can defer the work to 4.0. |
||
{ | ||
throw new InvalidOperationException( | ||
"Value-based sort cannot be rendered as a document. You might be trying to use a value-based sort where a field-based sort is expected."); | ||
} | ||
|
||
internal override BsonValue RenderAsBsonValue(RenderArgs<TDocument> args) => _direction.Render(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* Copyright 2010-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. | ||
*/ | ||
|
||
using System; | ||
using MongoDB.Bson; | ||
|
||
namespace MongoDB.Driver | ||
{ | ||
internal static class SortDirectionExtensions | ||
{ | ||
internal static BsonValue Render(this SortDirection direction) => | ||
direction switch | ||
{ | ||
SortDirection.Ascending => 1, | ||
SortDirection.Descending => -1, | ||
_ => throw new InvalidOperationException($"Invalid sort direction: {direction}.") | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using FluentAssertions; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization; | ||
|
@@ -31,6 +32,14 @@ public void Ascending() | |
Assert(subject.Ascending("a"), "{a: 1}"); | ||
} | ||
|
||
[Fact] | ||
public void Ascending_value() | ||
{ | ||
var subject = CreateSubject<BsonDocument>(); | ||
|
||
Assert(subject.Ascending(), "1"); | ||
} | ||
|
||
[Fact] | ||
public void Ascending_Typed() | ||
{ | ||
|
@@ -76,6 +85,16 @@ public void Combine_with_repeated_fields_using_extension_methods() | |
Assert(sort, "{b: -1, a: -1}"); | ||
} | ||
|
||
[Fact] | ||
public void Combine_with_value_based_sort_and_additional_sort_should_throw() | ||
{ | ||
var subject = CreateSubject<BsonDocument>(); | ||
|
||
var exception = Record.Exception(() => subject.Ascending().Descending("b")); | ||
|
||
exception.Should().BeOfType<InvalidOperationException>(); | ||
} | ||
|
||
[Fact] | ||
public void Descending() | ||
{ | ||
|
@@ -84,6 +103,14 @@ public void Descending() | |
Assert(subject.Descending("a"), "{a: -1}"); | ||
} | ||
|
||
[Fact] | ||
public void Descending_value() | ||
{ | ||
var subject = CreateSubject<BsonDocument>(); | ||
|
||
Assert(subject.Descending(), "-1"); | ||
} | ||
|
||
[Fact] | ||
public void Descending_Typed() | ||
{ | ||
|
@@ -117,10 +144,20 @@ public void MetaTextScore() | |
Assert(subject.MetaTextScore("awesome"), "{awesome: {$meta: 'textScore'}}"); | ||
} | ||
|
||
[Fact] | ||
public void CallingRenderOnValueBasedSortShouldThrow() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the underscores naming styling, like you did in |
||
{ | ||
var subject = CreateSubject<BsonDocument>(); | ||
|
||
var exception = Record.Exception(() => subject.Ascending().Render(new RenderArgs<BsonDocument>())); | ||
|
||
exception.Should().BeOfType<InvalidOperationException>(); | ||
} | ||
|
||
private void Assert<TDocument>(SortDefinition<TDocument> sort, string expectedJson) | ||
{ | ||
var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<TDocument>(); | ||
var renderedSort = sort.Render(new(documentSerializer, BsonSerializer.SerializerRegistry)); | ||
var renderedSort = sort.RenderAsBsonValue(new(documentSerializer, BsonSerializer.SerializerRegistry)); | ||
|
||
renderedSort.Should().Be(expectedJson); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
RenderAsBsonValue
method should probably be placed after theRender
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It already is? Maybe you got a defective diff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tagged the wrong one. It's the one on line 311 of SortDefinitionBuilder.cs that's out of order.