Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2929

Unable to use "First()" when sort by a property from a lookup foreign document

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.9.2
    • Component/s: Builders
    • Labels:
      None
    • Environment:
      Ubuntu 18.04 LTS

      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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            luizguilherme.goiana@gmail.com Luiz Guilherme Rodrigues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: