Skip to content

Commit 32ce2a0

Browse files
authored
Merge branch 'main' into DOCSP-37798-reads
2 parents f4b90b4 + f40fae8 commit 32ce2a0

File tree

5 files changed

+617
-1
lines changed

5 files changed

+617
-1
lines changed

snooty.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
55
"https://www.mongodb.com/docs/atlas/objects.inv"
66
]
77

8-
# toc_landing_pages = ["/paths/to/pages/that/have/nested/content"]
8+
toc_landing_pages = [
9+
"/tutorials/connect/",
10+
"/tutorials/write-ops/",
11+
]
912

1013
[constants]
1114
driver-short = "Java RS driver"

source/tutorials.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ Tutorials
1010
/tutorials/db-coll/
1111
/tutorials/indexes/
1212
/tutorials/read-ops/
13+
/tutorials/write-ops/
14+
/tutorials/aggregation/
1315

source/tutorials/aggregation.txt

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
.. _javars-aggregation:
2+
3+
=====================
4+
Aggregation Framework
5+
=====================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
The aggregation pipeline is a framework for data aggregation,
14+
modeled on the concept of data processing pipelines.
15+
16+
To learn more about aggregation, see :manual:`Aggregation Pipeline
17+
</core/aggregation-pipeline/>` in the Server manual.
18+
19+
Prerequisites
20+
-------------
21+
22+
You must set up the following components to run the code examples in
23+
this guide:
24+
25+
- A ``test.restaurants`` collection populated with documents from the
26+
``restaurants.json`` file in the `documentation assets GitHub
27+
<https://raw.githubusercontent.com/mongodb/docs-assets/drivers/restaurants.json>`__.
28+
29+
- The following import statements:
30+
31+
.. code-block:: java
32+
33+
import com.mongodb.reactivestreams.client.MongoClients;
34+
import com.mongodb.reactivestreams.client.MongoClient;
35+
import com.mongodb.reactivestreams.client.MongoCollection;
36+
import com.mongodb.reactivestreams.client.MongoDatabase;
37+
import com.mongodb.client.model.Aggregates;
38+
import com.mongodb.client.model.Accumulators;
39+
import com.mongodb.client.model.Projections;
40+
import com.mongodb.client.model.Filters;
41+
42+
import org.bson.Document;
43+
44+
.. important::
45+
46+
This guide uses the ``Subscriber`` implementations, which are
47+
described in the :ref:`Quick Start Primer <javars-primer>`.
48+
49+
Connect to a MongoDB Deployment
50+
-------------------------------
51+
52+
First, connect to a MongoDB deployment and declare and define
53+
``MongoDatabase`` and ``MongoCollection`` instances.
54+
55+
The following code connects to a standalone
56+
MongoDB deployment running on ``localhost`` on port ``27017``. Then, it
57+
defines the ``database`` variable to refer to the ``test`` database and
58+
the ``collection`` variable to refer to the ``restaurants`` collection:
59+
60+
.. code-block:: java
61+
62+
MongoClient mongoClient = MongoClients.create();
63+
MongoDatabase database = mongoClient.getDatabase("test");
64+
MongoCollection<Document> collection = database.getCollection("restaurants");
65+
66+
To learn more about connecting to MongoDB deployments,
67+
see the :ref:`javars-connect` tutorial.
68+
69+
Perform Aggregation
70+
-------------------
71+
72+
To perform aggregation, pass a list of aggregation stages to the
73+
``MongoCollection.aggregate()`` method. The driver provides the
74+
``Aggregates`` helper class that contains builders for aggregation
75+
stages.
76+
77+
In this example, the aggregation pipeline performs the
78+
following tasks:
79+
80+
- Uses a ``$match`` stage to filter for documents in which the
81+
``categories`` array field contains the element ``"Bakery"``. The example
82+
uses ``Aggregates.match()`` to build the ``$match`` stage.
83+
84+
+ Uses a ``$group`` stage to group the matching documents by
85+
the ``stars`` field, accumulating a count of documents for each distinct
86+
value of ``stars``. The example uses ``Aggregates.group()`` to build the
87+
``$group`` stage and ``Accumulators.sum()`` to build the accumulator
88+
expression. For the accumulator expressions for use within the
89+
``$group`` stage, the driver provides ``Accumulators`` helper
90+
class.
91+
92+
.. code-block:: java
93+
94+
collection.aggregate(
95+
Arrays.asList(
96+
Aggregates.match(Filters.eq("categories", "Bakery")),
97+
Aggregates.group("$stars", Accumulators.sum("count", 1))
98+
)
99+
).subscribe(new PrintDocumentSubscriber());
100+
101+
Use Aggregation Expressions
102+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
103+
104+
For ``$group`` accumulator expressions, the driver provides the
105+
``Accumulators`` helper class. For other aggregation expressions,
106+
manually build the expression by using the ``Document`` class.
107+
108+
In the following example, the aggregation pipeline uses a
109+
``$project`` stage to return only the ``name`` field and the calculated
110+
field ``firstCategory`` whose value is the first element in the
111+
``categories`` array. The example uses ``Aggregates.project()`` and
112+
various ``Projections`` class methods to build the ``$project`` stage:
113+
114+
.. code-block:: java
115+
116+
collection.aggregate(
117+
Arrays.asList(
118+
Aggregates.project(
119+
Projections.fields(
120+
Projections.excludeId(),
121+
Projections.include("name"),
122+
Projections.computed(
123+
"firstCategory",
124+
new Document("$arrayElemAt", Arrays.asList("$categories", 0))
125+
)
126+
)
127+
)
128+
)
129+
).subscribe(new PrintDocumentSubscriber());
130+
131+
Explain an Aggregation
132+
~~~~~~~~~~~~~~~~~~~~~~
133+
134+
To ``$explain`` an aggregation pipeline, call the
135+
``AggregatePublisher.explain()`` method:
136+
137+
.. code-block:: java
138+
139+
collection.aggregate(
140+
Arrays.asList(
141+
Aggregates.match(Filters.eq("categories", "Bakery")),
142+
Aggregates.group("$stars", Accumulators.sum("count", 1))))
143+
.explain()
144+
.subscribe(new PrintDocumentSubscriber());

source/tutorials/bulk-writes.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.. _javars-bulk-writes:
2+
3+
=====================
4+
Bulk Write Operations
5+
=====================
6+
7+
Starting in v2.6, MongoDB supports bulk write commands
8+
for insert, update, and delete operations in a way that allows the
9+
driver to implement the correct semantics for ``BulkWriteResult`` and
10+
``BulkWriteException``.
11+
12+
There are two types of bulk operations, ordered and unordered bulk
13+
operations:
14+
15+
1. Ordered bulk operations execute all the operations in order and
16+
error out on the first write error.
17+
#. Unordered bulk operations execute all the operations and report any
18+
the errors. Unordered bulk operations do not guarantee an order of
19+
execution.
20+
21+
.. important::
22+
23+
This guide uses the ``Subscriber`` implementations, which are
24+
described in the :ref:`Quick Start Primer <javars-primer>`.
25+
26+
The following code provides examples using ordered and unordered
27+
operations:
28+
29+
.. code-block:: java
30+
31+
// Ordered bulk operation - order is guaranteed
32+
collection.bulkWrite(
33+
Arrays.asList(new InsertOneModel<>(new Document("_id", 4)),
34+
new InsertOneModel<>(new Document("_id", 5)),
35+
new InsertOneModel<>(new Document("_id", 6)),
36+
new UpdateOneModel<>(new Document("_id", 1),
37+
new Document("$set", new Document("x", 2))),
38+
new DeleteOneModel<>(new Document("_id", 2)),
39+
new ReplaceOneModel<>(new Document("_id", 3),
40+
new Document("_id", 3).append("x", 4))))
41+
.subscribe(new ObservableSubscriber<BulkWriteResult>());
42+
43+
// Unordered bulk operation - no guarantee of order of operation
44+
collection.bulkWrite(
45+
Arrays.asList(new InsertOneModel<>(new Document("_id", 4)),
46+
new InsertOneModel<>(new Document("_id", 5)),
47+
new InsertOneModel<>(new Document("_id", 6)),
48+
new UpdateOneModel<>(new Document("_id", 1),
49+
new Document("$set", new Document("x", 2))),
50+
new DeleteOneModel<>(new Document("_id", 2)),
51+
new ReplaceOneModel<>(new Document("_id", 3),
52+
new Document("_id", 3).append("x", 4))),
53+
new BulkWriteOptions().ordered(false))
54+
.subscribe(new ObservableSubscriber<BulkWriteResult>());

0 commit comments

Comments
 (0)