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

Can't project only certain properties of an array using Fluent Find

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Builders
    • None

      Cluster: Standalone

      Edition: MongoDB 5.0.9 Community

      Summary

      Using the C# driver and LINQ Fluent Find, I'm not able to project only certain properties of an array.{}

      How to reproduce

      Let's say I have this data in my database:

      {
        "_id": {
          "$oid": "62baf9a2851424fdb8c226f8"
        },
        "Name": "Team A",
        "TeamData": {
          "Players": [
            {
              "FirstName": "First Name A",
              "LastName": "Last Name A"
            },
            {
              "FirstName": "First Name B",
              "LastName": "Last Name B"
            }
          ]
        }
      } 

      Using LINQ Fluent Find, I can't select an object containing only "LastName". By using this code:

      var result = coll.Find("{}")
         .Project(x => x.TeamData.Players.Select(c => c.LastName))
         .ToList() 

      The generated request for this ends up being:

      {
          "find": "coll",
          "filter": {},
          "projection": {
              "Players.LastName": 1,
              "TeamData.Players": 1,
              "_id": 0
          }
      } 

      ...which selects all of "Players" properties.

      Directly inputting the json works, but is not the desired way:

      var result = coll.Find("{}")
         .Project("{ 'TeamData.Players.LastName': 1 }")
         .ToList(); 

      Additional Background

      This spawned a question here.

            Assignee:
            Unassigned Unassigned
            Reporter:
            fhcimolin@gmail.com Flavio Cimolin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: