[CSHARP-2929] Unable to use "First()" when sort by a property from a lookup foreign document Created: 06/Feb/20  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Builders
Affects Version/s: 2.9.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Luiz Guilherme Rodrigues Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 18.04 LTS



 Description   

Hi Team,

i have to order a collection by a date attribute from a foreign collection.  I'm trying to do this:

await _context.MainCollection
.Aggregate()
.Match(mc => someIds.Contains(mc.Id)) //Only some registers
.Lookup<MainEntity, ForeignEntity, MainEntityWithForeign>(
_context.ForeignCollection,
mc => mc.Id,
fc => fc.MainCollectionId,
mewf => mewf.ForeignCollections
)
.SortByDescending(x => x.ForeignCollections.First().CreationDate)
 
but this does not work and my results are not ordered. The query generated considers my order attribute as part of the main collection instead of the foreign collection: { "$sort" : \{ "creationDate" : -1 } }
 
When I replace the method "First()" for "ElementAt(0)" its works as expected, and the generated query order command  is the one below:
{ "$sort" : \{ "foreignCollections.0.creationDate" : -1 } }
 
I think there is a bug when the driver tries to translate to the Mongo query when I use the method "First()".
 
Thank you.


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