|
10 | 10 | :depth: 2 |
11 | 11 | :class: singlecol |
12 | 12 |
|
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: code example, query, aggregation |
| 19 | + |
13 | 20 |
|
14 | 21 | Overview |
15 | 22 | -------- |
@@ -575,3 +582,82 @@ implementation of LINQ: |
575 | 582 |
|
576 | 583 | * - ``Where`` |
577 | 584 | - Returns all documents that match your specified criteria |
| 585 | + |
| 586 | +View Translated Queries |
| 587 | +----------------------- |
| 588 | + |
| 589 | +When you run a LINQ query, the {+driver-short+} automatically translates your |
| 590 | +query into an aggregation pipeline written with the {+query-api+}. You can view |
| 591 | +the translated query by using the ``ToString()`` method or the |
| 592 | +``LoggedStages`` property. |
| 593 | + |
| 594 | +To see the translated query for **non-scalar operations**, use the ``ToString()`` |
| 595 | +method. Non-scalar operations are operations that return a query object, such |
| 596 | +as: |
| 597 | + |
| 598 | +- ``Where`` |
| 599 | +- ``Select`` |
| 600 | +- ``SelectMany`` |
| 601 | +- ``GroupJoin`` |
| 602 | + |
| 603 | +The following example calls the ``ToString()`` method on a LINQ query and prints |
| 604 | +the translated query: |
| 605 | + |
| 606 | +.. io-code-block:: |
| 607 | + |
| 608 | + .. input:: |
| 609 | + :language: csharp |
| 610 | + |
| 611 | + var queryableCollection = _restaurantsCollection.AsQueryable(); |
| 612 | + var query = queryableCollection |
| 613 | + .Where(r => r.Name == "The Movable Feast"); |
| 614 | + |
| 615 | + var queryTranslated = query.ToString(); |
| 616 | + Console.WriteLine(queryTranslated); |
| 617 | + |
| 618 | + .. output:: |
| 619 | + |
| 620 | + sample_restaurants.restaurants.Aggregate([{ "$match" : { "name" : "The Movable Feast" } }]) |
| 621 | + |
| 622 | +To get the translated query for **scalar operations** use the ``LoggedStages`` |
| 623 | +property. Scalar operations are operations that return a scalar result rather than a |
| 624 | +query object, such as: |
| 625 | + |
| 626 | +- ``First`` |
| 627 | +- ``Sum`` |
| 628 | +- ``Count`` |
| 629 | +- ``Min`` |
| 630 | +- ``Max`` |
| 631 | + |
| 632 | +To get a translated query with the ``LoggedStages`` property, you must save |
| 633 | +the translated query directly after it is executed, and before executing any |
| 634 | +other queries with the same queryable object. |
| 635 | + |
| 636 | +The following example uses the ``LoggedStages`` property on a LINQ query that |
| 637 | +uses a scalar operation, then prints the translated query: |
| 638 | + |
| 639 | +.. io-code-block:: |
| 640 | + |
| 641 | + .. input:: |
| 642 | + :language: csharp |
| 643 | + :emphasize-lines: 6 |
| 644 | + |
| 645 | + |
| 646 | + var queryableCollection = _restaurantsCollection.AsQueryable(); |
| 647 | + var query = queryableCollection |
| 648 | + .Where(r => r.Name == "The Movable Feast"); |
| 649 | + |
| 650 | + var result = query.FirstOrDefault(); |
| 651 | + var queryTranslated = query.LoggedStages; |
| 652 | + |
| 653 | + Console.WriteLine(queryTranslated.ToJson()); |
| 654 | + |
| 655 | + .. output:: |
| 656 | + |
| 657 | + [{ "$match" : { "name" : "The Movable Feast" } }, { "$limit" : NumberLong(1) }] |
| 658 | + |
| 659 | +.. important:: |
| 660 | + |
| 661 | + ``LoggedStages`` is not thread-safe. Executing a query and accessing the |
| 662 | + associated ``LoggedStages`` property from multiple threads might have |
| 663 | + non-deterministic results. |
0 commit comments