[CSHARP-2711] only fields are allowed in a $sort when ordering by a nullable field Created: 25/Aug/19  Updated: 12/Nov/21  Resolved: 25/Sep/19

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.9.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Andrej Zolger Assignee: Dmitry Lukyanov (Inactive)
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Azure cloud (Windows)


Issue Links:
Duplicate
duplicates CSHARP-3965 OrderBy with an expression that doesn... Closed

 Description   

I am using OData against the MongoDB IQueryable collection and it works fine but when I try to sort by a nullable field it breaks with: only fields are allowed in a $sort when ordering by a nullable field

If I convert that field to not nullable it works fine.

 
api/TimeTracking/ItemType?$orderBy=ModifiedAt => error when nullable
api/TimeTracking/ItemType?$orderBy=ModifiedAt => working when not nullable

IMongoQueryable<T> query = queryOptions == null? entities.AsQueryable() : queryOptions.ApplyTo(entities.AsQueryable()) as IMongoQueryable<T>;
 

Error:

Exceptions.ErrorHandlingMiddleware:Error: Only fields are allowed in a $sort.
 
System.NotSupportedException: Only fields are allowed in a $sort.
   at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateOrderBy(OrderByExpression node)
   at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node)
   at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
   at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at MongoDB.Driver.Linq.MongoQueryableImpl`2.ToCursorAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)

{{}}

Did anyone have a similar experience? Is there a workaround for this?

{{}}

Nuget:

{{}}

  • Microsoft.AspNetCore.OData v:7.1.0
  • MongoDB.Driver v:2.9.1

{{}}



 Comments   
Comment by Dmitry Lukyanov (Inactive) [ 25/Sep/19 ]

Feel free to reopen this ticket if it's still relevant for you

Comment by Dmitry Lukyanov (Inactive) [ 18/Sep/19 ]

Hello zolgera81@gmail.com,

I've tried to reproduce your situation, but without any results.
Can you please provide the following information (as many as you can):
1. The generated linq query which triggers this error: `Only fields are allowed in a $sort` (without `OData` specific things).
2. The structure of classes which are mentioned in the url: `api/TimeTracking/ItemType?$orderBy=ModifiedAt`. Ideally, it will be great, if you can provide a small console application which reproduces this problem.
3. it will not be superfluous if you can provide 1-2 db records which you expect to see in the result of api/TimeTracking/ItemType?$orderBy=ModifiedAt

Generated at Wed Feb 07 21:43:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.