@@ -346,6 +346,96 @@ following documents:
346
346
{ "date" : ISODate("2012-12-05T00:00:00Z"), "grade" : "A", "score" : 13 }
347
347
{ "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 }
348
348
349
+ Nested Statements
350
+ +++++++++++++++++
351
+
352
+ You can chain or nest ``Select`` and ``SelectMany`` statements to unwind nested
353
+ arrays. Consider a collection that contains documents with a **new** schema. These
354
+ documents contain a ``restaurants`` field, which holds an array of documents
355
+ represented by the ``Restaurant`` class. The documents within the array each have
356
+ a ``grades`` field that holds an array of documents represented by
357
+ the ``Grade`` class. The following code is an example of a single document in
358
+ this collection:
359
+
360
+ .. code-block:: json
361
+
362
+ {
363
+ "_id": { "$oid": ... },
364
+ "restaurants": [
365
+ {
366
+ "_id": { ... } ,
367
+ "address": { ... },
368
+ "name": "Tov Kosher Kitchen",
369
+ "grades": [
370
+ {
371
+ "date" : ISODate("2014-11-24T00:00:00Z"),
372
+ "grade" : "Z",
373
+ "score" : 20.0
374
+ },
375
+ {
376
+ "date" : ISODate("2013-01-17T00:00:00Z"),
377
+ "grade" : "A",
378
+ "score" : 13.0
379
+ }
380
+ ]
381
+ ...
382
+ },
383
+ {
384
+ "_id": { ... } ,
385
+ "address": { ... },
386
+ "name": "Harriet's Kitchen",
387
+ "grades": [
388
+ {
389
+ "date" : ISODate("2014-04-19T00:00:00Z"),
390
+ "grade" : "B",
391
+ "score" : 12.0
392
+ }
393
+ ],
394
+ ...
395
+ },
396
+ ...
397
+ ]
398
+ }
399
+
400
+ You can nest ``SelectMany`` statements within ``SelectMany`` or ``Select``
401
+ statements. The following example nests a ``SelectMany`` statement within a
402
+ ``Select`` statement to retrieve an array from each document in the collection.
403
+ Each array holds all grade objects from all restaurants in each document.
404
+
405
+ .. io-code-block::
406
+ :copyable: true
407
+
408
+ .. input:: /includes/fundamentals/code-examples/linq.cs
409
+ :language: csharp
410
+ :start-after: start-nested-SelectMany
411
+ :end-before: end-nested-SelectMany
412
+
413
+ .. output::
414
+ :visible: false
415
+ :language: json
416
+
417
+ // output for first document in collection
418
+ [
419
+ { "date" : ISODate("2014-11-24T00:00:00Z"),
420
+ "grade" : "Z",
421
+ "score" : 20.0
422
+ },
423
+ { "date" : ISODate("2013-01-17T00:00:00Z"),
424
+ "grade" : "A",
425
+ "score" : 13.0
426
+ },
427
+ {
428
+ "date" : ISODate("2014-04-19T00:00:00Z"),
429
+ "grade" : "B",
430
+ "score" : 12.0
431
+ },
432
+ ...
433
+ ],
434
+ // output for second document in collection
435
+ [
436
+ ...
437
+ ]
438
+
349
439
$group
350
440
~~~~~~
351
441
0 commit comments