|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using FluentAssertions; |
| 19 | +using MongoDB.Bson; |
| 20 | +using MongoDB.Driver.Linq; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 24 | +{ |
| 25 | + public class CSharp2723Tests |
| 26 | + { |
| 27 | + [Fact] |
| 28 | + public void Nested_Select_should_work() |
| 29 | + { |
| 30 | + var client = DriverTestConfiguration.Linq3Client; |
| 31 | + var database = client.GetDatabase("foo"); |
| 32 | + var parents = database.GetCollection<Parent>("parents"); |
| 33 | + var children = database.GetCollection<Child>("children"); |
| 34 | + var grandChildren = database.GetCollection<GrandChild>("grandChildren"); |
| 35 | + |
| 36 | + database.DropCollection("parents"); |
| 37 | + database.DropCollection("children"); |
| 38 | + database.DropCollection("grandChildren"); |
| 39 | + |
| 40 | + parents.InsertMany(new[] |
| 41 | + { |
| 42 | + new Parent { Id = 1, Name = "parent1" }, |
| 43 | + new Parent { Id = 2, Name = "parent2" } |
| 44 | + }); |
| 45 | + |
| 46 | + children.InsertMany(new[] |
| 47 | + { |
| 48 | + new Child { Id = 1, Name = "child1", ParentId = 2 }, |
| 49 | + new Child { Id = 2, Name = "child2", ParentId = 1 } |
| 50 | + }); |
| 51 | + |
| 52 | + grandChildren.InsertMany(new[] |
| 53 | + { |
| 54 | + new GrandChild { Id = 1, Name = "grandchild1", ChildId = 2 }, |
| 55 | + new GrandChild { Id = 2, Name = "grandchild2", ChildId = 1 } |
| 56 | + }); |
| 57 | + |
| 58 | + var aggregate = parents |
| 59 | + .Aggregate() |
| 60 | + .Lookup<Parent, Child, ParentProjection>( |
| 61 | + children, |
| 62 | + parent => parent.Id, |
| 63 | + child => child.ParentId, |
| 64 | + parentProjection => parentProjection.Children) |
| 65 | + .Lookup<ParentProjection, GrandChild, GrandChild, IEnumerable<GrandChild>, ParentProjection>( |
| 66 | + grandChildren, |
| 67 | + let: new BsonDocument { { "children", "$Children" } }, |
| 68 | + lookupPipeline: new BsonDocumentStagePipelineDefinition<GrandChild, GrandChild>( |
| 69 | + new[] { BsonDocument.Parse(@"{ $match : { $expr : { $and : [ { $in : [ ""$ChildId"", ""$$children._id"" ] } ] } } }") }), |
| 70 | + childProjection => childProjection.GrandChildren) |
| 71 | + .Project(parent => new ParentTree |
| 72 | + { |
| 73 | + Id = parent.Id, |
| 74 | + ParentName = parent.Name, |
| 75 | + Children = parent.Children |
| 76 | + .Select(child => new ChildTree |
| 77 | + { |
| 78 | + Id = child.Id, |
| 79 | + Name = child.Name, |
| 80 | + GrandChildren = parent |
| 81 | + .GrandChildren |
| 82 | + .Where(gc => |
| 83 | + gc.ChildId == |
| 84 | + child.Id) |
| 85 | + .Select(gc => |
| 86 | + new GrandChildProjection() |
| 87 | + { |
| 88 | + Id = gc.Id, |
| 89 | + Name = gc.Name |
| 90 | + }) |
| 91 | + }) |
| 92 | + }); |
| 93 | + |
| 94 | + var stages = Linq3TestHelpers.Translate(parents, aggregate); |
| 95 | + var expectedStages = new[] |
| 96 | + { |
| 97 | + @"{ |
| 98 | + '$lookup':{ |
| 99 | + 'from':'children', |
| 100 | + 'localField':'_id', |
| 101 | + 'foreignField':'ParentId', |
| 102 | + 'as':'Children' |
| 103 | + } |
| 104 | + }", |
| 105 | + @"{ |
| 106 | + '$lookup':{ |
| 107 | + 'from':'grandChildren', |
| 108 | + 'let':{'children':'$Children'}, |
| 109 | + 'pipeline':[ |
| 110 | + { |
| 111 | + '$match':{'$expr':{'$and':[{'$in':['$ChildId','$$children._id']}]}}} |
| 112 | + ], |
| 113 | + 'as':'GrandChildren' |
| 114 | + } |
| 115 | + }", |
| 116 | + @"{ |
| 117 | + '$project':{ |
| 118 | + '_id':'$_id', |
| 119 | + 'ParentName':'$Name', |
| 120 | + 'Children':{ |
| 121 | + '$map':{ |
| 122 | + 'input':'$Children', |
| 123 | + 'as':'child', |
| 124 | + 'in':{ |
| 125 | + '_id':'$$child._id', |
| 126 | + 'Name':'$$child.Name', |
| 127 | + 'GrandChildren':{ |
| 128 | + '$map':{ |
| 129 | + 'input':{ |
| 130 | + '$filter':{ |
| 131 | + 'input':'$GrandChildren', |
| 132 | + 'as':'gc', |
| 133 | + 'cond':{ |
| 134 | + '$eq':[ |
| 135 | + '$$gc.ChildId', |
| 136 | + '$$child._id' |
| 137 | + ] |
| 138 | + } |
| 139 | + } |
| 140 | + }, |
| 141 | + 'as':'gc', |
| 142 | + 'in':{ |
| 143 | + '_id':'$$gc._id', |
| 144 | + 'Name':'$$gc.Name' |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + }" |
| 153 | + }; |
| 154 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 155 | + |
| 156 | + var pipelineDefinition = new BsonDocumentStagePipelineDefinition<Parent, BsonDocument>(expectedStages.Select(s => BsonDocument.Parse(s))); |
| 157 | + var resultBsonDocuments = parents.Aggregate(pipelineDefinition).ToList(); |
| 158 | + var expectedBsonDocuments = new[] |
| 159 | + { |
| 160 | + "{ '_id' : 1, 'ParentName' : 'parent1', 'Children' : [{ '_id' : 2, 'Name' : 'child2', 'GrandChildren' : [{ '_id' : 1, 'Name' : 'grandchild1' }] }] }", |
| 161 | + "{ '_id' : 2, 'ParentName' : 'parent2', 'Children' : [{ '_id' : 1, 'Name' : 'child1', 'GrandChildren' : [{ '_id' : 2, 'Name' : 'grandchild2' }] }] }" |
| 162 | + }; |
| 163 | + resultBsonDocuments.Should().Equal(expectedBsonDocuments.Select(d => BsonDocument.Parse(d))); |
| 164 | + |
| 165 | + var result = aggregate.ToList(); |
| 166 | + var expectedResults = new[] |
| 167 | + { |
| 168 | + new ParentTree { Id = 1, ParentName = "parent1", Children = new[] { new ChildTree { Id = 2, Name = "child2", GrandChildren = new[] { new GrandChildProjection { Id = 1, Name = "grandchild1" } } } } }, |
| 169 | + new ParentTree { Id = 2, ParentName = "parent2", Children = new[] { new ChildTree { Id = 1, Name = "child1", GrandChildren = new[] { new GrandChildProjection { Id = 2, Name = "grandchild2" } } } } } |
| 170 | + }; |
| 171 | + var parentTreeComparer = new ParentTreeComparer(); |
| 172 | + result.Should().Equal(expectedResults, (x, y) => parentTreeComparer.Equals(x, y)); |
| 173 | + } |
| 174 | + |
| 175 | + public class Parent |
| 176 | + { |
| 177 | + public int Id { get; set; } |
| 178 | + public string Name { get; set; } |
| 179 | + } |
| 180 | + |
| 181 | + public class Child |
| 182 | + { |
| 183 | + public int Id { get; set; } |
| 184 | + public string Name { get; set; } |
| 185 | + public int ParentId { get; set; } |
| 186 | + } |
| 187 | + |
| 188 | + public class GrandChild |
| 189 | + { |
| 190 | + public int Id { get; set; } |
| 191 | + public string Name { get; set; } |
| 192 | + public int ChildId { get; set; } |
| 193 | + } |
| 194 | + |
| 195 | + public class ParentProjection |
| 196 | + { |
| 197 | + public int Id { get; set; } |
| 198 | + public string Name { get; set; } |
| 199 | + public IEnumerable<Child> Children { get; set; } |
| 200 | + public IEnumerable<GrandChild> GrandChildren { get; set; } |
| 201 | + } |
| 202 | + |
| 203 | + public class GrandChildProjection |
| 204 | + { |
| 205 | + public int Id { get; set; } |
| 206 | + public string Name { get; set; } |
| 207 | + } |
| 208 | + |
| 209 | + public class ParentTree |
| 210 | + { |
| 211 | + public int Id { get; set; } |
| 212 | + public string ParentName { get; set; } |
| 213 | + public IEnumerable<ChildTree> Children { get; set; } |
| 214 | + } |
| 215 | + |
| 216 | + public class ChildTree |
| 217 | + { |
| 218 | + public int Id { get; set; } |
| 219 | + public string Name { get; set; } |
| 220 | + public IEnumerable<GrandChildProjection> GrandChildren { get; set; } |
| 221 | + } |
| 222 | + |
| 223 | + private class ParentTreeComparer : IEqualityComparer<ParentTree> |
| 224 | + { |
| 225 | + private readonly IEqualityComparer<ChildTree> _childTreeComparer = new ChildTreeComparer(); |
| 226 | + |
| 227 | + public bool Equals(ParentTree x, ParentTree y) |
| 228 | + { |
| 229 | + return |
| 230 | + x.Id == y.Id && |
| 231 | + x.ParentName == y.ParentName && |
| 232 | + x.Children.SequenceEqual(y.Children, _childTreeComparer); |
| 233 | + } |
| 234 | + |
| 235 | + public int GetHashCode(ParentTree obj) => throw new System.NotImplementedException(); |
| 236 | + } |
| 237 | + |
| 238 | + private class ChildTreeComparer : IEqualityComparer<ChildTree> |
| 239 | + { |
| 240 | + private readonly IEqualityComparer<GrandChildProjection> _grandChildProjectionComparer = new GrandChildProjectionComparer(); |
| 241 | + |
| 242 | + public bool Equals(ChildTree x, ChildTree y) |
| 243 | + { |
| 244 | + return |
| 245 | + x.Id == y.Id && |
| 246 | + x.Name == y.Name && |
| 247 | + x.GrandChildren.SequenceEqual(y.GrandChildren, _grandChildProjectionComparer); |
| 248 | + } |
| 249 | + |
| 250 | + public int GetHashCode(ChildTree obj) => throw new System.NotImplementedException(); |
| 251 | + } |
| 252 | + |
| 253 | + private class GrandChildProjectionComparer : IEqualityComparer<GrandChildProjection> |
| 254 | + { |
| 255 | + public bool Equals(GrandChildProjection x, GrandChildProjection y) |
| 256 | + { |
| 257 | + return |
| 258 | + x.Id == y.Id && |
| 259 | + x.Name == y.Name; |
| 260 | + } |
| 261 | + |
| 262 | + public int GetHashCode(GrandChildProjection obj) => throw new System.NotImplementedException(); |
| 263 | + } |
| 264 | + } |
| 265 | +} |
0 commit comments