Skip to content

Commit 1087a56

Browse files
(DOCSP-19772): VSCode export to language (#46)
* (DOCSP-19772): VSCode export to language * updates per copy review * word tweak * Apply suggestions from code review Co-authored-by: Anna Henningsen <[email protected]> * adjustment Co-authored-by: Anna Henningsen <[email protected]>
1 parent 9370c03 commit 1087a56

8 files changed

+230
-8
lines changed

source/export-to-language.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.. _vsce-export-to-language:
2+
3+
======================================
4+
Export a Query or Pipeline to Language
5+
======================================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
You can export and translate query documents and aggregation pipelines
16+
from a :ref:`playground <vsce-playgrounds>` into a programming language.
17+
You can export queries and pipelines to the following languages:
18+
19+
- C#
20+
- Java
21+
- Node.js
22+
- Python
23+
24+
Prerequisites
25+
-------------
26+
27+
You must open a playground that contains a query document or pipeline
28+
you want to export.
29+
30+
The tutorials on this page use the default playground template.
31+
32+
To open a new playground containing the default template:
33+
34+
.. include:: /includes/steps/open-new-playground.rst
35+
36+
Export a Query Document
37+
-----------------------
38+
39+
To export a query document:
40+
41+
.. include:: /includes/steps/export-to-language-query.rst
42+
43+
Export an Aggregation Pipeline
44+
------------------------------
45+
46+
To export an aggregation pipeline:
47+
48+
.. include:: /includes/steps/export-to-language-pipeline.rst
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
To use this example, **start with a blank MongoDB Playground** by
2-
clearing the template Playground if it is loaded.
1+
To run this example, **start with a blank MongoDB Playground** by
2+
clearing the template Playground if it is loaded.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
You can choose whether to include import statements, driver syntax,
2+
or both in your exported code.
3+
4+
At the top of the newly opened VS Code window containing your
5+
exported code, use the :guilabel:`Import Statements` and
6+
:guilabel:`Driver Syntax` toggles to control these options.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a. When you highlighted your code, a light bulb icon appeared. Click
2+
the icon.
3+
4+
#. In the context menu, choose the language you want to export to.
5+
|vsce| opens a new VS Code window containing the highlighted code
6+
in your chosen language.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
title: Highlight the code you want to export.
2+
level: 4
3+
ref: highlight-code
4+
content: |
5+
6+
Highlight the aggregation pipeline from the playground template:
7+
8+
.. code-block:: javascript
9+
10+
[
11+
{ $match: { date: { $gte: new Date('2014-01-01'), $lt: new Date('2015-01-01') } } },
12+
{ $group: { _id: '$item', totalSaleAmount: { $sum: { $multiply: [ '$price', '$quantity' ] } } } }
13+
]
14+
15+
---
16+
title: Export your selection.
17+
level: 4
18+
ref: export-selection
19+
content: |
20+
21+
.. include:: /includes/fact-export-selection.rst
22+
23+
For example, exporting the pipeline from Step 1 to Java results in
24+
the following code:
25+
26+
.. code-block:: java
27+
28+
Arrays.asList(new Document("$match",
29+
new Document("date",
30+
new Document("$gte",
31+
new java.util.Date(1388534400000L))
32+
.append("$lt",
33+
new java.util.Date(1420070400000L)))),
34+
new Document("$group",
35+
new Document("_id", "$item")
36+
.append("totalSaleAmount",
37+
new Document("$sum",
38+
new Document("$multiply", Arrays.asList("$price", "$quantity"))))))
39+
---
40+
title: Configure Export Options
41+
level: 4
42+
ref: export-options
43+
content: |
44+
45+
.. include:: /includes/fact-export-options.rst
46+
47+
Including both import statements and driver syntax for the preceding
48+
Java code results in this output:
49+
50+
.. code-block:: java
51+
52+
import java.util.Arrays;
53+
import org.bson.Document;
54+
import com.mongodb.MongoClient;
55+
import com.mongodb.MongoClientURI;
56+
import com.mongodb.client.FindIterable;
57+
import com.mongodb.client.MongoCollection;
58+
import com.mongodb.client.MongoDatabase;
59+
import org.bson.conversions.Bson;
60+
import java.util.concurrent.TimeUnit;
61+
import org.bson.Document;
62+
63+
/*
64+
* Requires the MongoDB Java Driver.
65+
* https://mongodb.github.io/mongo-java-driver
66+
*/
67+
68+
MongoClient mongoClient = new MongoClient(
69+
new MongoClientURI(
70+
"mongodb://localhost:27017/?readPreference=primary&appname=mongodb-vscode+0.7.0&directConnection=true&ssl=false"
71+
)
72+
);
73+
MongoDatabase database = mongoClient.getDatabase("mongodbVSCodePlaygroundDB");
74+
MongoCollection<Document> collection = database.getCollection("sales");
75+
76+
FindIterable<Document> result = collection.aggregate(Arrays.asList(new Document("$match",
77+
new Document("date",
78+
new Document("$gte",
79+
new java.util.Date(1388534400000L))
80+
.append("$lt",
81+
new java.util.Date(1420070400000L)))),
82+
new Document("$group",
83+
new Document("_id", "$item")
84+
.append("totalSaleAmount",
85+
new Document("$sum",
86+
new Document("$multiply", Arrays.asList("$price", "$quantity")))))));
87+
88+
.. note::
89+
90+
Export options vary by the selected export language.
91+
92+
...
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
title: Highlight the code you want to export.
2+
level: 4
3+
ref: highlight-code
4+
content: |
5+
6+
Highlight the query document from the playground template:
7+
8+
.. code-block:: json
9+
10+
{ date: { $gte: new Date('2014-04-04'), $lt: new Date('2014-04-05') } }
11+
12+
---
13+
title: Export your selection.
14+
level: 4
15+
ref: export-selection
16+
content: |
17+
18+
.. include:: /includes/fact-export-selection.rst
19+
20+
For example, exporting the query document from Step 1 to Java results
21+
in the following code:
22+
23+
.. code-block:: java
24+
25+
new Document("date", new Document("$gte", new java.util.Date(1396569600000L))
26+
.append("$lt", new java.util.Date(1396656000000L)))
27+
---
28+
title: Configure Export Options
29+
level: 4
30+
ref: export-options
31+
content: |
32+
33+
.. include:: /includes/fact-export-options.rst
34+
35+
Including both import statements and driver syntax
36+
for the preceding Java code results in this output:
37+
38+
.. code-block:: java
39+
40+
import org.bson.Document;
41+
import com.mongodb.MongoClient;
42+
import com.mongodb.MongoClientURI;
43+
import com.mongodb.client.FindIterable;
44+
import com.mongodb.client.MongoCollection;
45+
import com.mongodb.client.MongoDatabase;
46+
import org.bson.conversions.Bson;
47+
import java.util.concurrent.TimeUnit;
48+
import org.bson.Document;
49+
50+
/*
51+
* Requires the MongoDB Java Driver.
52+
* https://mongodb.github.io/mongo-java-driver
53+
*/
54+
55+
MongoClient mongoClient = new MongoClient(
56+
new MongoClientURI(
57+
"mongodb://localhost:27017/?readPreference=primary&appname=mongodb-vscode+0.7.0&directConnection=true&ssl=false"
58+
)
59+
);
60+
MongoDatabase database = mongoClient.getDatabase("mongodbVSCodePlaygroundDB");
61+
MongoCollection<Document> collection = database.getCollection("sales");
62+
63+
FindIterable<Document> result = collection.aggregate(new Document("date", new Document("$gte", new java.util.Date(1396569600000L))
64+
.append("$lt", new java.util.Date(1396656000000L))));
65+
66+
.. note::
67+
68+
Export options vary by the selected export language.
69+
70+
...

source/includes/steps-open-new-playground.yaml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ content: |
2121
:guilabel:`MongoDB:`.
2222
2323
When you run the :guilabel:`MongoDB: Create MongoDB Playground`
24-
command, |vsce| opens a playground pre-configured with a few
25-
commands.
24+
command, |vsce| opens a default playground template pre-configured
25+
with a few commands.
2626
2727
.. note::
2828
29-
You can load new Playgrounds without the template by
30-
disabling the :guilabel:`Use Default Template For Playground`
31-
setting. To learn more about |vsce| settings, see
32-
:ref:`vsce-settings`.
29+
To load new Playgrounds without the template, disable the
30+
:guilabel:`Use Default Template For Playground` setting. To learn
31+
more about |vsce| settings, see :ref:`vsce-settings`.
3332
3433
...

source/playgrounds.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,5 @@ Consideration for Authentication
210210

211211
/crud-ops
212212
/run-agg-pipelines
213+
/export-to-language
213214
/require-modules

0 commit comments

Comments
 (0)