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

Nested select which reference parent select actually still references child

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 2.14.0
    • Affects Version/s: 2.6.0
    • Component/s: Linq

      Consider the program below:

      static void Main()
      {
      		var server = new MongoClient(new MongoUrl("mongodb://localhost:27017"));
      		var db = server.GetDatabase("FooBar"); 
      		var foos = db.GetCollection<FooNest>("Foos");
      		
      		foos.DeleteMany((x) => true);
      		
      		foos.InsertOne(
      			new FooNest{ 
      				Name = "Parent", 
      				NestedCollection = new []{
      					new FooNest { 
      						Name = "Child"
      					}
      				}
      			});
      		
      		var itms2 = foos.AsQueryable()
      			.Select(top => top.NestedCollection
      				.Select(child => new {ParentName = top.Name, child.Name}));
      				
      		Console.WriteLine("Items returned: " + itms2.ToList().ToJson());
      		Console.WriteLine();
      		Console.WriteLine("Query translation: " + itms2.ToString());
      }
      
      class FooNest
      {
      	public string Name;
      	public IEnumerable<FooNest> NestedCollection;
      }
      

      The result of running this program is:

      Items returned: [[{ "ParentName" : "Child", "Name" : "Child" }]]
      

      It seems that the driver does not take into account that we want the Name property of the top object instead of the child object. The translation is printed out like this:

      Query translation: aggregate([{ "$project" : { "__fld0" : { "$map" : { "input" : "$NestedCollection", "as" : "child", "in" : { "ParentName" : "$$child.Name", "Name" : "$$child.Name" } } }, "_id" : 0 } }])
      

      Obviously the $map operator has no reference to the "parent" object. This behavior is wrong. The best would be if the driver used the "parent" object. But if that is not possible it would be better that the driver threw an error.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            mortb Mårten Byström
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: