Skip to content

Commit 9da3483

Browse files
authored
P2: Usage examples moved to appropriate pages (#624)
1 parent 44b324c commit 9da3483

File tree

12 files changed

+339
-279
lines changed

12 files changed

+339
-279
lines changed

source/command.txt

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,24 @@
22
Run a Command
33
=============
44

5-
You can run all raw database operations using the
5+
.. facet::
6+
:name: genre
7+
:values: reference
8+
9+
.. meta::
10+
:keywords: database, call, admin, administration, meta, backend, configure
11+
:description: Learn about how to run database commands in the {+driver-long+}.
12+
13+
.. contents:: On this page
14+
:local:
15+
:backlinks: none
16+
:depth: 2
17+
:class: singlecol
18+
19+
Overview
20+
--------
21+
22+
In this guide, you can learn how to run raw database operations using the
623
``MongoDatabase.runCommand()`` method. A raw database operation is a
724
command you can execute directly on the {+mdb-server+} CLI. These
825
commands include administrative and diagnostic tasks, such as fetching
@@ -28,17 +45,16 @@ By default, ``runCommand`` returns an object of type
2845
can specify a return type for ``runCommand()`` as an optional second
2946
parameter.
3047

31-
Example
32-
-------
48+
Run Command Example: Full File
49+
------------------------------
3350

3451
In the following sample code, we send the ``dbStats`` command to request
3552
statistics from a specific MongoDB database.
3653

3754
.. include:: /includes/connect-guide-note.rst
3855

39-
.. literalinclude:: /includes/usage-examples/code-snippets/Command.java
40-
:language: java
41-
56+
.. literalinclude:: /includes/RunCommand.java
57+
:language: java
4258

4359
When you run the preceding command, you should see output similar to the
4460
following:
@@ -50,9 +66,18 @@ following:
5066

5167
.. include:: /includes/legacy-redirect.rst
5268

53-
For additional information on the classes and methods mentioned on this
54-
page, see the following resources:
69+
Additional Information
70+
----------------------
71+
72+
API Documentation
73+
~~~~~~~~~~~~~~~~~
74+
75+
For more information about the method used to run database commands, see the following API documentation:
76+
77+
- `runCommand() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoDatabase.html#runCommand(org.bson.conversions.Bson)>`__
78+
79+
Server Manual Entries
80+
~~~~~~~~~~~~~~~~~~~~~
5581

56-
- `runCommand() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoDatabase.html#runCommand(org.bson.conversions.Bson)>`__ API Documentation
57-
- :manual:`Database Commands </reference/command/>` Server Manual Entry
58-
- :manual:`dbStats </reference/command/dbStats/>` Server Manual Entry
82+
- :manual:`Database Commands </reference/command/>`
83+
- :manual:`dbStats </reference/command/dbStats/>`

source/crud/read-operations.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ Read Operations
1111
:caption: Read Operations
1212

1313
Retrieve Data </crud/read-operations/retrieve>
14-
Data from a Cursor </crud/read-operations/cursor>
14+
Access Data from a Cursor </crud/read-operations/cursor>
1515
Sort Results </crud/read-operations/sort>
1616
Skip Returned Results </crud/read-operations/skip>
1717
Limit Returned Results </crud/read-operations/limit>
1818
Specify Fields to Return </crud/read-operations/project>
19+
Count Documents </crud/read-operations/count>
20+
Retrieve Distinct Values of a Field </crud/read-operations/distinct>
1921
Geospatial Data </crud/read-operations/geo>
2022
Search Text </crud/read-operations/text>
2123

22-
- :doc:`/crud/read-operations/retrieve`
23-
- :doc:`/crud/read-operations/cursor`
24-
- :doc:`/crud/read-operations/sort`
25-
- :doc:`/crud/read-operations/skip`
26-
- :doc:`/crud/read-operations/limit`
27-
- :doc:`/crud/read-operations/project`
28-
- :doc:`/crud/read-operations/geo`
29-
- :doc:`/crud/read-operations/text`
24+
- :ref:`Retrieve <java-fundamentals-retrieve-data>`
25+
- :ref:`Access Data From a Cursor <java-fundamentals-cursor>`
26+
- :ref:`Sort Results <java-fundamentals-sort>`
27+
- :ref:`Skip Returned Results <java-fundamentals-skip>`
28+
- :ref:`Limit the Number of Returned Results <java-fundamentals-limit>`
29+
- :ref:`Specify Which Fields to Return <java-fundamentals-project>`
30+
- :ref:`Count Documents <java-count>`
31+
- :ref:`Retrieve Distinct Values of a Field <java-usage-distinct>`
32+
- :ref:`Search Geospatially <java-geo-search>`
33+
- :ref:`Search Text <java-fundamentals-search-text>`

source/usage-examples/count.txt renamed to source/crud/read-operations/count.txt

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
.. _java-usage-count:
1+
.. _java-count:
22

33
===============
44
Count Documents
55
===============
66

7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: count, number, size, collection, code example
13+
:description: Learn about how to count the number of documents in a collection in the {+driver-long+}.
14+
15+
.. contents:: On this page
16+
:local:
17+
:backlinks: none
18+
:depth: 2
19+
:class: singlecol
720

21+
Overview
22+
--------
823

9-
There are two instance methods in the ``MongoCollection`` class that you can
10-
call to count the number of documents in a collection:
24+
In this guide, you can learn how to count the number of documents in your
25+
MongoDB collections. There are two instance methods in the
26+
``MongoCollection`` class that you can call to count the number of documents in
27+
a collection:
1128

1229
- ``countDocuments()`` returns the number of documents in the collection
1330
that match a specified query. If you specify an empty query filter,
@@ -74,17 +91,19 @@ specify the behavior of the call:
7491

7592
Both methods return the number of matching documents as a ``long`` primitive.
7693

77-
Example
78-
-------
94+
.. _java-usage-count:
95+
96+
Count Documents Example: Full File
97+
----------------------------------
98+
99+
.. include:: /includes/crud/example-intro.rst
79100

80101
The following example estimates the number of documents in the
81102
``movies`` collection in the ``sample_mflix`` database, and then returns
82103
an accurate count of the number of documents in the ``movies``
83104
collection with ``Canada`` in the ``countries`` field.
84105

85-
.. include:: /includes/connect-guide-note.rst
86-
87-
.. literalinclude:: /includes/usage-examples/code-snippets/CountDocuments.java
106+
.. literalinclude:: /includes/crud/CountDocuments.java
88107
:language: java
89108

90109
If you run the preceding sample code, you should see output that looks something
@@ -98,10 +117,21 @@ like this (exact numbers can vary depending on your data):
98117

99118
.. include:: /includes/legacy-redirect.rst
100119

101-
For additional information on the classes and methods mentioned on this
102-
page, see the following API Documentation:
120+
Additional Information
121+
----------------------
122+
123+
API Documentation
124+
~~~~~~~~~~~~~~~~~
125+
126+
For more information about the classes and methods used to count documents, see the following API documentation:
103127

104128
- `countDocuments() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#countDocuments()>`__
105129
- `estimatedDocumentCount() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#estimatedDocumentCount()>`__
106130
- `CountOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/CountOptions.html>`__
107131
- `EstimatedDocumentCountOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/EstimatedDocumentCountOptions.html>`__
132+
133+
Server Manual Entries
134+
~~~~~~~~~~~~~~~~~~~~~
135+
136+
- :manual:`db.collection.countDocuments() </reference/method/db.collection.countDocuments/>`
137+
- :manual:`db.collection.estimatedDocumentCount() </reference/method/db.collection.estimatedDocumentCount/>`

source/usage-examples/distinct.txt renamed to source/crud/read-operations/distinct.txt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,28 @@
44
Retrieve Distinct Values of a Field
55
===================================
66

7+
.. facet::
8+
:name: genre
9+
:values: reference
710

11+
.. meta::
12+
:keywords: find, distinct, unique, discrete, code example
13+
:description: Learn about how to retrieve the number of distinct values of a field in the {+driver-long+}.
814

9-
You can retrieve a list of distinct values for a field across a
10-
collection by calling the ``distinct()`` method on a ``MongoCollection``
11-
object. Pass the document field name as the first parameter and the class
12-
you want to cast the results to as the second parameter as shown below:
15+
.. contents:: On this page
16+
:local:
17+
:backlinks: none
18+
:depth: 2
19+
:class: singlecol
20+
21+
Overview
22+
--------
23+
24+
In this guide, you can learn how to retrieve a list of distinct values for a
25+
field across a collection by calling the ``distinct()`` method on a
26+
``MongoCollection`` object. Pass the document field name as the first parameter
27+
and the class you want to use for the results as the second parameter, as shown
28+
below:
1329

1430
.. code-block:: java
1531

@@ -48,7 +64,7 @@ match movies that include "Carl Franklin" as one of the values in the
4864

4965
.. include:: /includes/connect-guide-note.rst
5066

51-
.. literalinclude:: /includes/usage-examples/code-snippets/Distinct.java
67+
.. literalinclude:: /includes/crud/Distinct.java
5268
:language: java
5369

5470
When you run the example, you should see output that reports each distinct
@@ -66,7 +82,7 @@ which resembles the following:
6682

6783
.. include:: /includes/legacy-redirect.rst
6884

69-
For additional information on the classes and methods mentioned on this
85+
For more information about the classes and methods mentioned on this
7086
page, see the following resources:
7187

7288
- `distinct() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#distinct(java.lang.String,java.lang.Class)>`__ API Documentation

source/crud/read-operations/geo.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
.. _java-geo-search:
2+
13
===================
24
Search Geospatially
35
===================
46

7+
.. facet::
8+
:name: genre
9+
:values: reference
510

11+
.. meta::
12+
:keywords: find, geo, geographic, location, locale, coordinate, latitude, longitude, code example
13+
:description: Learn about how to search geospatial data in the {+driver-long+}.
614

715
.. contents:: On this page
816
:local:

source/includes/usage-examples/code-snippets/Command.java renamed to source/includes/RunCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Runs a database command by using the Java driver
22

3-
package usage.examples;
3+
package org.example;
44

55
import org.bson.BsonDocument;
66
import org.bson.BsonInt64;

source/includes/usage-examples/code-snippets/CountDocuments.java renamed to source/includes/crud/CountDocuments.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// Runs count operations on a collection by using the Java driver
1+
/**
2+
* This file demonstrates how to open a change stream by using the Java driver.
3+
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and listens
4+
* to change events in the "movies" collection. The code uses a change stream with a pipeline
5+
* to only filter for "insert" and "update" events.
6+
*/
27

38
package usage.examples;
49

@@ -24,19 +29,13 @@ public static void main(String[] args) {
2429

2530
Bson query = eq("countries", "Spain");
2631

27-
try {
28-
// Retrieves and prints the estimated number of documents in the collection
29-
long estimatedCount = collection.estimatedDocumentCount();
30-
System.out.println("Estimated number of documents in the movies collection: " + estimatedCount);
31-
32-
// Retrieves and prints the number of documents with a "countries" value of "Spain"
33-
long matchingCount = collection.countDocuments(query);
34-
System.out.println("Number of movies from Spain: " + matchingCount);
35-
36-
// Prints a message if any exceptions occur during the operations
37-
} catch (MongoException me) {
38-
System.err.println("An error occurred: " + me);
39-
}
32+
// Retrieves and prints the estimated number of documents in the collection
33+
long estimatedCount = collection.estimatedDocumentCount();
34+
System.out.println("Estimated number of documents in the movies collection: " + estimatedCount);
35+
36+
// Retrieves and prints the number of documents with a "countries" value of "Spain"
37+
long matchingCount = collection.countDocuments(query);
38+
System.out.println("Number of movies from Spain: " + matchingCount);
4039
}
4140
}
4241
}

source/includes/usage-examples/code-snippets/Distinct.java renamed to source/includes/crud/Distinct.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,13 @@ public static void main(String[] args) {
2222
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2323
MongoCollection<Document> collection = database.getCollection("movies");
2424

25-
try {
26-
// Retrieves the distinct values of the "year" field present in documents that match the filter
27-
DistinctIterable<Integer> docs = collection.distinct("year", Filters.eq("directors", "Carl Franklin"), Integer.class);
28-
MongoCursor<Integer> results = docs.iterator();
29-
30-
// Prints the distinct "year" values
31-
while(results.hasNext()) {
32-
System.out.println(results.next());
33-
}
34-
35-
// Prints a message if any exceptions occur during the operation
36-
} catch (MongoException me) {
37-
System.err.println("An error occurred: " + me);
25+
// Retrieves the distinct values of the "year" field present in documents that match the filter
26+
DistinctIterable<Integer> docs = collection.distinct("year", Filters.eq("directors", "Carl Franklin"), Integer.class);
27+
MongoCursor<Integer> results = docs.iterator();
28+
29+
// Prints the distinct "year" values
30+
while(results.hasNext()) {
31+
System.out.println(results.next());
3832
}
3933
}
4034
}

source/includes/usage-examples/code-snippets/Watch.java renamed to source/includes/crud/Watch.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* to only filter for "insert" and "update" events.
66
*/
77

8-
package usage.examples;
8+
package org.example;
99

1010
import java.util.Arrays;
1111
import java.util.List;
@@ -20,6 +20,7 @@
2020
import com.mongodb.client.MongoDatabase;
2121
import com.mongodb.client.model.Filters;
2222
import com.mongodb.client.model.changestream.FullDocument;
23+
import com.mongodb.client.model.Aggregates;
2324

2425
public class Watch {
2526
public static void main( String[] args ) {
@@ -33,21 +34,21 @@ public static void main( String[] args ) {
3334

3435
// Creates instructions to match insert and update operations
3536
List<Bson> pipeline = Arrays.asList(
36-
Aggregates.match(
37-
Filters.in("operationType",
38-
Arrays.asList("insert", "update"))));
39-
37+
Aggregates.match(
38+
Filters.in("operationType",
39+
Arrays.asList("insert", "update"))));
40+
4041
// Creates a change stream that receives change events for the specified operations
4142
ChangeStreamIterable<Document> changeStream = database.watch(pipeline)
42-
.fullDocument(FullDocument.UPDATE_LOOKUP);
43-
43+
.fullDocument(FullDocument.UPDATE_LOOKUP);
44+
4445
final int[] numberOfEvents = {0};
4546

4647
// Prints a message each time the change stream receives a change event, until it receives two events
4748
changeStream.forEach(event -> {
48-
System.out.println("Received a change to the collection: " + event);
49+
System.out.println("Received a change to the collection: " + event);
4950
if (++numberOfEvents[0] >= 2) {
50-
System.exit(0);
51+
System.exit(0);
5152
}
5253
});
5354
}

0 commit comments

Comments
 (0)