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

LINQ GroupJoin Does Not Support $lookup with array of values

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Gone away
    • Icon: Minor - P4 Minor - P4
    • None
    • None
    • None
    • None

    Description

      Summary

      The `$lookup` aggregation pipeline step supports referencing a local field that contains an array of items. The `GroupJoin` method does not support this scenario, instead requiring that the local field is a single value.

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

       

      This applies to version 2.22.0.

      How to Reproduce

      Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

       

      Given the following sample collections:

       

      people: [
        {
          _id: new ObjectId("6537c2058a094813badf7b12"),
          name: "Jessie"
          organizations: [new ObjectId("6537c2058a094813badf7b11")]
        }
      ]
       
      organizations: [
        {
          _id: new ObjecId("6537c2058a094813badf7b11"),
          name: "Sample"
        }
      ]

       

       

      I can write the following aggregation pipeline:

       

      db.people.aggregate([
          {
              $lookup: {
                  from: "organizations",
                  localField: "organizations",
                  foreignField: "_id",
                  as: "organizations"
              }
          }
      ]) 

       

       

      And retrieve a result that looks like:

       

      [
        {
          _id: new ObjectId("6537c2058a094813badf7b12"),
          name: "Jessie"
          organizations: [
            {
              _id: new ObjectId("6537c2058a094813badf7b11"),
              name: "Sample"
            }
          ]
        }
      ]

       

      Using LINQ, I would expect to be able to do the following:

      class PersonDto 
      {
          public ObjectId Id { get; set; }
          public string Name { get; set; }
          public List<ObjectId> Organizations { get; set; }
      }
       
      class Organization
      {
          public ObjectId { get; set; }
          public string Name { get; set; }
      }
       
      class Person
      {
          public ObjectId { get; set; }
          public string Name { get; set; }
          public List<Organization> Organizations { get; set; }
      }
       
      // This snippet below assumes the presence of the classes above
       
      var peopleCollection = mongoDbClient.GetDatabase("example").GetCollection<PersonDto>("people");
      var organizationsCollection = mongoDbClient.GetDatabase("example").GetCollection<PersonDto>("organizations");
       
      var person = await peopleCollection.AsQueryable()
          .GroupJoin<PersonDto, Organization, ObjectId, Person>(organizationsCollection.AsQueryable(),
              // compilation error on this line: Cannot convert expression 
              // type 'System.Collections.Generic.List<MongoDB.Bson.ObjectId>' 
              // to return type 'MongoDB.Bson.ObjectId'
              person => person.Organizations,
              organization => organization.Id,
              (person, organizations) => new Person {
                  Id = person.Id,
                  Name = person.Name,
                  Organizations = organizations.ToList()
               }
          )
          .FirstOrDefaultAsync();

       

      Additional Background

      Please provide any additional background information that may be helpful in diagnosing the bug.

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            scott@freshli.io M. Scott Ford
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: