@@ -160,6 +160,138 @@ public async Task Should_translate_and_flattened()
160
160
result . Value . Result . Should ( ) . BeFalse ( ) ;
161
161
}
162
162
163
+ [ Test ]
164
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
165
+ public async Task Should_translate_arrayElemAt_using_a_constant_ElementAt ( )
166
+ {
167
+ var result = await Project ( x => new { Result = x . M . ElementAt ( 1 ) } ) ;
168
+
169
+ result . Projection . Should ( ) . Be ( "{ Result: { $arrayElemAt: [\" $M\" , 1] }, _id: 0 }" ) ;
170
+
171
+ result . Value . Result . Should ( ) . Be ( 4 ) ;
172
+ }
173
+
174
+ [ Test ]
175
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
176
+ public async Task Should_translate_arrayElemAt_using_a_constant_indexer ( )
177
+ {
178
+ var result = await Project ( x => new { Result = x . M [ 1 ] } ) ;
179
+
180
+ result . Projection . Should ( ) . Be ( "{ Result: { $arrayElemAt: [\" $M\" , 1] }, _id: 0 }" ) ;
181
+
182
+ result . Value . Result . Should ( ) . Be ( 4 ) ;
183
+ }
184
+
185
+ [ Test ]
186
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
187
+ public async Task Should_translate_arrayElemAt_using_a_constant_get_Item ( )
188
+ {
189
+ var result = await Project ( x => new { Result = x . O [ 1 ] } ) ;
190
+
191
+ result . Projection . Should ( ) . Be ( "{ Result: { $arrayElemAt: [\" $O\" , 1] }, _id: 0 }" ) ;
192
+
193
+ result . Value . Result . Should ( ) . Be ( 20 ) ;
194
+ }
195
+
196
+ [ Test ]
197
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
198
+ public async Task Should_translate_arrayElemAt_using_a_variable_ElementAt ( )
199
+ {
200
+ var result = await Project ( x => new { Result = ( int ? ) x . M . ElementAt ( x . T [ "one" ] ) } ) ;
201
+
202
+ result . Projection . Should ( ) . Be ( "{ Result: { $arrayElemAt: [\" $M\" , \" $T.one\" ] }, _id: 0 }" ) ;
203
+
204
+ result . Value . Result . Should ( ) . Be ( 4 ) ;
205
+ }
206
+
207
+ [ Test ]
208
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
209
+ public async Task Should_translate_arrayElemAt_using_a_variable_indexer ( )
210
+ {
211
+ var result = await Project ( x => new { Result = ( int ? ) x . M [ x . T [ "one" ] ] } ) ;
212
+
213
+ result . Projection . Should ( ) . Be ( "{ Result: { $arrayElemAt: [\" $M\" , \" $T.one\" ] }, _id: 0 }" ) ;
214
+
215
+ result . Value . Result . Should ( ) . Be ( 4 ) ;
216
+ }
217
+
218
+ [ Test ]
219
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
220
+ public async Task Should_translate_arrayElemAt_using_a_variable_get_Item ( )
221
+ {
222
+ var result = await Project ( x => new { Result = ( long ? ) x . O [ x . T [ "one" ] ] } ) ;
223
+
224
+ result . Projection . Should ( ) . Be ( "{ Result: { $arrayElemAt: [\" $O\" , \" $T.one\" ] }, _id: 0 }" ) ;
225
+
226
+ result . Value . Result . Should ( ) . Be ( 20 ) ;
227
+ }
228
+
229
+ [ Test ]
230
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
231
+ public async Task Should_translate_arrayElemAt_using_a_constant_ElementAt_followed_by_a_field ( )
232
+ {
233
+ var result = await Project ( x => new { Result = x . G . ElementAt ( 1 ) . D } ) ;
234
+
235
+ result . Projection . Should ( ) . Be ( "{ Result: { $let: { vars: { item: { \" $arrayElemAt\" : [\" $G\" , 1] } }, in: \" $$item.D\" } }, _id: 0 }" ) ;
236
+
237
+ result . Value . Result . Should ( ) . Be ( "Dolphin" ) ;
238
+ }
239
+
240
+ [ Test ]
241
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
242
+ public async Task Should_translate_arrayElemAt_using_a_variable_ElementAt_followed_by_a_field ( )
243
+ {
244
+ var result = await Project ( x => new { Result = x . G . ElementAt ( x . T [ "one" ] ) . D } ) ;
245
+
246
+ result . Projection . Should ( ) . Be ( "{ Result: { $let: { vars: { item: { \" $arrayElemAt\" : [\" $G\" , \" $T.one\" ] } }, in: \" $$item.D\" } }, _id: 0 }" ) ;
247
+
248
+ result . Value . Result . Should ( ) . Be ( "Dolphin" ) ;
249
+ }
250
+
251
+ [ Test ]
252
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
253
+ public async Task Should_translate_arrayElemAt_using_First ( )
254
+ {
255
+ var result = await Project ( x => new { Result = x . M . First ( ) } ) ;
256
+
257
+ result . Projection . Should ( ) . Be ( "{ Result: { \" $arrayElemAt\" : [\" $M\" , 0] }, _id: 0 }" ) ;
258
+
259
+ result . Value . Result . Should ( ) . Be ( 2 ) ;
260
+ }
261
+
262
+ [ Test ]
263
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
264
+ public async Task Should_translate_arrayElemAt_using_First_followed_by_a_field ( )
265
+ {
266
+ var result = await Project ( x => new { Result = x . G . First ( ) . D } ) ;
267
+
268
+ result . Projection . Should ( ) . Be ( "{ Result: { $arrayElemAt: [\" $G.D\" , 0] }, _id: 0 }" ) ;
269
+
270
+ result . Value . Result . Should ( ) . Be ( "Don't" ) ;
271
+ }
272
+
273
+ [ Test ]
274
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
275
+ public async Task Should_translate_arrayElemAt_using_Last ( )
276
+ {
277
+ var result = await Project ( x => new { Result = x . M . Last ( ) } ) ;
278
+
279
+ result . Projection . Should ( ) . Be ( "{ Result: { \" $arrayElemAt\" : [\" $M\" , -1] }, _id: 0 }" ) ;
280
+
281
+ result . Value . Result . Should ( ) . Be ( 5 ) ;
282
+ }
283
+
284
+ [ Test ]
285
+ [ RequiresServer ( MinimumVersion = "3.1.7" ) ]
286
+ public async Task Should_translate_arrayElemAt_using_Last_followed_by_a_field ( )
287
+ {
288
+ var result = await Project ( x => new { Result = x . G . Last ( ) . D } ) ;
289
+
290
+ result . Projection . Should ( ) . Be ( "{ Result: { \" $arrayElemAt\" : [\" $G.D\" , -1] }, _id: 0 }" ) ;
291
+
292
+ result . Value . Result . Should ( ) . Be ( "Dolphin" ) ;
293
+ }
294
+
163
295
[ Test ]
164
296
[ RequiresServer ( MinimumVersion = "3.1.7" ) ]
165
297
public async Task Should_translate_avg ( )
@@ -1139,15 +1271,15 @@ private async Task<ProjectedResult<TResult>> Project<TResult>(Expression<Func<Ro
1139
1271
var projectionInfo = AggregateProjectTranslator . Translate ( projector , serializer , BsonSerializer . SerializerRegistry ) ;
1140
1272
1141
1273
var pipelineOperator = new BsonDocument ( "$project" , projectionInfo . Document ) ;
1142
- using ( var cursor = await _collection . AggregateAsync ( new PipelineStagePipelineDefinition < Root , TResult > ( new PipelineStageDefinition < Root , TResult > [ ] { pipelineOperator } , projectionInfo . ProjectionSerializer ) ) )
1274
+ var result = await _collection . Aggregate ( )
1275
+ . Project ( new BsonDocumentProjectionDefinition < Root , TResult > ( projectionInfo . Document , projectionInfo . ProjectionSerializer ) )
1276
+ . FirstAsync ( ) ;
1277
+
1278
+ return new ProjectedResult < TResult >
1143
1279
{
1144
- var list = await cursor . ToListAsync ( ) ;
1145
- return new ProjectedResult < TResult >
1146
- {
1147
- Projection = projectionInfo . Document ,
1148
- Value = ( TResult ) list [ 0 ]
1149
- } ;
1150
- }
1280
+ Projection = projectionInfo . Document ,
1281
+ Value = result
1282
+ } ;
1151
1283
}
1152
1284
1153
1285
private class ProjectedResult < T >
0 commit comments