[CSHARP-2743] GraphLookup cannot connect one-to-many Created: 13/Sep/19  Updated: 28/Oct/23  Resolved: 01/Jul/20

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

Type: New Feature Priority: Major - P3
Reporter: Bouke Haarsma Assignee: Mikalai Mazurenka (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I'm trying to do a graph lookup, but it looks like the C# API doesn't agree with my thinking here:

class Node
{
    string Id { get; set; }
    List<string> ParentIds { get; set; }
}
 
class NodeWithChildren
{
    Node[] Children { get; set; }
}
 
var query = collection.Aggregate()
     .GraphLookup(
          from: collection,
          connectFromField: x => x.Id,
          connectToField: x => x.ParentIds,
          startWith: x => x.Id,
          @as: (NodeWithChildren x) => x.Children);

This throws:

 
ArgumentException:  TConnectFrom must be either TConnectTo or a type that implements IEnumerable<TConnectTo>.
Parameter name: TConnectFrom

While the query should work fine:

 db.Node.aggregate([
     { $graphLookup: { from: 'Node', startWith: "$_id", connectFromField: "_id", connectToField: "ParentIds", as: "Children" } }
])



 Comments   
Comment by Githook User [ 01/Jul/20 ]

Author:

{'name': 'Mikalai Mazurenka', 'email': 'mikalai.mazurenka@mongodb.com', 'username': 'MikalaiMazurenka'}

Message: CSHARP-2743: Update type validation and add tests
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/21b6b06833dce7abf259b401a381fcd5ef2188ad

Comment by Githook User [ 01/Jul/20 ]

Author:

{'name': 'TimTim Wong', 'email': 't2@live.hk'}

Message: CSHARP-2743: Fix type assertion for $graphLookup
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/22eb59ffc2a524db628b49e4af4dafd458b89aa8

Comment by TimTIM Wong [ 06/May/20 ]

I've created a pull request just now https://github.com/mongodb/mongo-csharp-driver/pull/402

Comment by TimTIM Wong [ 06/May/20 ]

Rather than a feature suggestion, it is a bug.

PipelineStageDefinitionBuilder.cs lines 492-493 should also accept the reverse as below:
492. TConnectFrom must be either TConnectTo or a type that implements IEnumerable<TConnectTo> unless TConnectTo is a type that implements IEnumerable<TConnectFrom>
493. TStartWith must be either TConnectFrom or a type that implements IEnumerable<TConnectFrom> unless TConnectFrom is a type that implements IEnumerable<TStartWith>

Comment by Bouke Haarsma [ 23/Sep/19 ]

This report is neither a question nor a support discussion. It is reporting a feature suggestion for a shortcoming in the C# driver.

Comment by Esha Bhargava [ 23/Sep/19 ]

bhaarsma@yellowtail.nl Thanks for your report. Please note that this project is for reporting bugs or feature suggestions for the driver. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-users group.

Comment by Bouke Haarsma [ 13/Sep/19 ]

Sorry for the formatting, I tried. I can't edit the description anymore. Here's a repost with (hopefully) correct formatting:

I'm trying to do a graph lookup, but it looks like the C# API doesn't agree with my thinking here:

class Node
{
    string Id { get; set; }
    List<string> ParentIds { get; set; }
}
class NodeWithChildren
{
    Node[] Children { get; set; }
}
 
var query = collection.Aggregate()
    .GraphLookup(
        from: collection,
        connectFromField: x => x.Id,
        connectToField: x => x.ParentIds,
        startWith: x => x.Id,
        @as: (NodeWithChildren x) => x.Children);

This throws:

ArgumentException: 
TConnectFrom must be either TConnectTo or a type that implements IEnumerable<TConnectTo>.Parameter name: TConnectFrom

While the query should work fine:

db.Node.aggregate([
    {
        $graphLookup: {
              from: 'Node',
              startWith: "$_id",
              connectFromField: "_id",
              connectToField: "ParentIds",
              as: "Children"
           }
    },
])

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