-
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
8d9710e
67ef77e
f0ec8c0
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 |
---|---|---|
|
@@ -131,7 +131,18 @@ public static SortDefinition<TDocument> MetaTextScore<TDocument>(this SortDefini | |
public sealed class SortDefinitionBuilder<TDocument> | ||
{ | ||
/// <summary> | ||
/// Creates an ascending sort. | ||
/// Creates an ascending sort on a value rather than on a field of a document. For example, "$sort : 1". | ||
/// This is used when sorting primitive values like strings or numbers, but can also be used to sort whole documents. | ||
/// </summary> | ||
BorisDog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// <returns>A value ascending sort.</returns> | ||
public SortDefinition<TDocument> Ascending() | ||
{ | ||
return new ValueDirectionalSortDefinition<TDocument>(SortDirection.Ascending); | ||
} | ||
|
||
/// <summary> | ||
/// Creates an ascending sort based on a specific field within the document. For example, "$sort : { field : 1 }". | ||
/// This is used when values are documents, and you want to sort by a particular field's value. | ||
/// </summary> | ||
/// <param name="field">The field.</param> | ||
/// <returns>An ascending sort.</returns> | ||
|
@@ -141,7 +152,8 @@ public SortDefinition<TDocument> Ascending(FieldDefinition<TDocument> field) | |
} | ||
|
||
/// <summary> | ||
/// Creates an ascending sort. | ||
/// Creates an ascending sort based on a specific field within the document. For example, "$sort : { field : 1 }". | ||
/// This is used when values are documents, and you want to sort by a particular field's value. | ||
/// </summary> | ||
/// <param name="field">The field.</param> | ||
/// <returns>An ascending sort.</returns> | ||
|
@@ -171,7 +183,18 @@ public SortDefinition<TDocument> Combine(IEnumerable<SortDefinition<TDocument>> | |
} | ||
|
||
/// <summary> | ||
/// Creates a descending sort. | ||
/// Creates a descending sort on a value rather than on a field of a document. For example, "$sort : -1". | ||
/// This is used when sorting primitive values like strings or numbers, but can also be used to sort whole documents. | ||
/// </summary> | ||
/// <returns>A value descending sort.</returns> | ||
public SortDefinition<TDocument> Descending() | ||
{ | ||
return new ValueDirectionalSortDefinition<TDocument>(SortDirection.Descending); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a descending sort based on a specific field within the document. For example, "$sort: { field: -1 }". | ||
/// This is used when values are documents, and you want to sort by a particular field's value. | ||
/// </summary> | ||
/// <param name="field">The field.</param> | ||
/// <returns>A descending sort.</returns> | ||
|
@@ -181,7 +204,8 @@ public SortDefinition<TDocument> Descending(FieldDefinition<TDocument> field) | |
} | ||
|
||
/// <summary> | ||
/// Creates a descending sort. | ||
/// Creates a descending sort based on a specific field within the document. For example, "$sort: { field: -1 }". | ||
/// This is used when values are documents, and you want to sort by a particular field's value. | ||
/// </summary> | ||
/// <param name="field">The field.</param> | ||
/// <returns>A descending sort.</returns> | ||
|
@@ -232,6 +256,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 +301,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}.") | ||
}; | ||
} | ||
} |
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.