Skip to content

Commit 1b43c37

Browse files
committed
retrieve
1 parent 024cb76 commit 1b43c37

File tree

5 files changed

+71
-23
lines changed

5 files changed

+71
-23
lines changed

source/crud/delete.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ For additional information on the methods and classes used to delete documents,
184184
- `findOneAndDelete() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#findOneAndDelete(org.bson.conversions.Bson)>`__
185185
- `DeleteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteOptions.html>`__
186186
- `FindOneAndDeleteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/FindOneAndDeleteOptions.html>`__
187+
- `DeleteResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/DeleteResult.html>`__
187188

188189
Server Manual Entries
189190
---------------------
190191

191-
- :manual:`db.collection.deleteOne() </reference/method/db.collection.deleteMany/>`
192-
- :manual:`db.collection.deleteMany() </reference/method/db.collection.deleteOne/>`
192+
- :manual:`db.collection.deleteOne() </reference/method/db.collection.deleteOne/>`
193+
- :manual:`db.collection.deleteMany() </reference/method/db.collection.deleteMany/>`
193194
- :manual:`db.collection.findOneAndDelete() </reference/method/db.collection.findOneAndDelete/>`

source/crud/query-documents/find.txt

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
Find Documents
55
==============
66

7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: find, findOne, findMany, get, lookup
13+
:description: Learn about how to retrieve data in the {+driver-long+}.
14+
715
.. contents:: On this page
816
:local:
917
:backlinks: none
@@ -80,8 +88,27 @@ The following shows the output of the preceding query:
8088
After the owner runs this query, they find two orders that matched the
8189
criteria.
8290

83-
For a runnable ``find()`` example, see our :ref:`Find Multiple
84-
Documents <retrieve-find>` page.
91+
Find Example: Full File
92+
~~~~~~~~~~~~~~~~~~~~~~~
93+
94+
The following code is a complete, standalone file that performs a find one
95+
operation and a find many operation.
96+
97+
.. include:: /includes/crud/example-intro.rst
98+
99+
.. io-code-block::
100+
101+
.. input:: /includes/crud/Find.java
102+
:language: java
103+
:dedent:
104+
105+
.. output::
106+
:language: none
107+
:visible: false
108+
109+
Number of documents found with find(): 101
110+
111+
Document found with find().first(){"title": "The Room", "imdb": {"rating": 3.5, "votes": 25673, "id": 368226}}
85112

86113
.. _retrieve-aggregate:
87114

@@ -97,7 +124,8 @@ instance of a ``MongoCollection``. This method accepts aggregation
97124
expressions to run in sequence. To perform aggregations, you can
98125
define aggregation stages that specify how to match documents, rename
99126
fields, and group values. For more information, see our
100-
:ref:`Aggregation <java-aggregation>` guide.
127+
:ref:`Aggregation <java-aggregation>` guide and the :ref:`Aggregation Expression
128+
Operations <java-aggregation-expression-operations>` page.
101129

102130
Example
103131
~~~~~~~
@@ -132,8 +160,21 @@ purchased color.
132160
For more information about how to construct an aggregation pipeline, see
133161
the {+mdb-server+} manual page on :manual:`Aggregation </aggregation>`.
134162

135-
For additional information on the methods mentioned on this page, see
136-
the following API Documentation:
163+
API Documentation
164+
-----------------
165+
166+
For additional information on the methods and classes used to retrieve documents
167+
on this page, see the following API Documentation:
168+
169+
- `find() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#find()>`__
170+
- `first() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#first()>`__
171+
- `FindIterable <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html>`__
172+
- `aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
173+
174+
Server Manual Entries
175+
---------------------
137176

138-
- `MongoCollection.find() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#find()>`__
139-
- `MongoCollection.aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
177+
- :manual:`Collections </core/databases-and-collections/#collections>`
178+
- :manual:`Query Documents </tutorial/query-documents>`
179+
- :manual:`Aggregation </aggregation>`
180+
- :manual:`Aggregation stages </meta/aggregation-quick-reference/#stages>`

source/crud/update-documents.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ operation and an update many operation.
182182

183183
.. io-code-block::
184184

185-
.. input:: /includes/crud/Insert.java
185+
.. input:: /includes/crud/Update.java
186186
:language: java
187187
:dedent:
188188

@@ -292,14 +292,13 @@ documents match.
292292
ReplaceOne Example: Full File
293293
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
294294

295-
The following code is a complete, standalone file that performs an update one
296-
operation and an update many operation.
295+
The following code is a complete, standalone file that performs a replace one operation.
297296

298297
.. include:: /includes/crud/example-intro.rst
299298

300299
.. io-code-block::
301300

302-
.. input:: /includes/crud/Replace.java
301+
.. input:: /includes/crud/ReplaceOne.java
303302
:language: java
304303
:dedent:
305304

@@ -322,7 +321,7 @@ For additional information on the methods and classes used to modify documents,
322321
- `UpdateOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOptions.html>`__
323322
- `replaceOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#replaceOne(org.bson.conversions.Bson,TDocument)>`__
324323
- `ReplaceOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOptions.html?is-external=true>`__
325-
- - `UpdateResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/UpdateResult.html>`__
324+
- `UpdateResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/UpdateResult.html>`__
326325

327326
For additional information on the methods used in the examples on this
328327
page, see the following API Documentation:

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
// Retrieves documents that match a query filter by using the Java driver
2-
3-
package usage.examples;
4-
52
import static com.mongodb.client.model.Filters.lt;
63

74
import org.bson.Document;
@@ -15,6 +12,8 @@
1512
import com.mongodb.client.model.Projections;
1613
import com.mongodb.client.model.Sorts;
1714

15+
import static com.mongodb.client.model.Filters.eq;
16+
1817
public class Find {
1918
public static void main( String[] args ) {
2019

@@ -36,12 +35,20 @@ public static void main( String[] args ) {
3635
.sort(Sorts.descending("title")).iterator();
3736

3837
// Prints the results of the find operation as JSON
39-
try {
40-
while(cursor.hasNext()) {
41-
System.out.println(cursor.next().toJson());
42-
}
43-
} finally {
44-
cursor.close();
38+
System.out.println("Number of documents found with find(): " + cursor.available() + "\n");
39+
cursor.close();
40+
41+
// Retrieves the first matching document, applying a projection and a descending sort to the results
42+
Document doc = collection.find(eq("title", "The Room"))
43+
.projection(projectionFields)
44+
.sort(Sorts.descending("imdb.rating"))
45+
.first();
46+
47+
// Prints a message if there are no result documents, or prints the result document as JSON
48+
if (doc == null) {
49+
System.out.println("No results found.");
50+
} else {
51+
System.out.println("Document found with find().first()" + doc.toJson());
4552
}
4653
}
4754
}

0 commit comments

Comments
 (0)