Skip to content

Commit 0175d7a

Browse files
DOCSP-37640 Document loggedStages (#205)
1 parent faded52 commit 0175d7a

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ int-data-type = "``integer``"
3838
not-available = "N/A"
3939
analyzer = "MongoDB C# Analyzer"
4040
analyzer-short = "C# Analzyer"
41+
query-api = "MongoDB Query API"

source/fundamentals/builders.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ can use for the following tasks:
3131

3232
The {+analyzer-short+} is a tool that helps you analyze your
3333
builders definitions and understand how your {+lang-framework+} code
34-
translates into the MongoDB Query API. For more information and
34+
translates into the {+query-api+}. For more information and
3535
installation instructions, see the `{+analyzer-short+} reference page <https://www.mongodb.com/docs/mongodb-analyzer/current/>`__.
3636

3737
You should read this guide if you want to learn more about how to

source/fundamentals/linq.txt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ LINQ
1010
:depth: 2
1111
:class: singlecol
1212

13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: code example, query, aggregation
19+
1320

1421
Overview
1522
--------
@@ -575,3 +582,82 @@ implementation of LINQ:
575582

576583
* - ``Where``
577584
- 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.

source/index.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,6 @@ To learn more, see the
141141
-------------------
142142

143143
The {+analyzer-short+} is a tool that helps you understand how your
144-
{+driver-short+} code translates into the MongoDB Query API and if your code
144+
{+driver-short+} code translates into the {+query-api+} and if your code
145145
includes any unsupported LINQ or builder expressions. To learn more, see the
146146
`{+analyzer-short+} documentation <https://www.mongodb.com/docs/mongodb-analyzer>`__.

0 commit comments

Comments
 (0)