|
| 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 | +... |
0 commit comments