Skip to content

Commit 9dadaf1

Browse files
committed
delete
1 parent a63df06 commit 9dadaf1

File tree

5 files changed

+68
-172
lines changed

5 files changed

+68
-172
lines changed

source/crud/write-operations/bulk.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,22 +364,20 @@ BulkWrite Example File
364364
~~~~~~~~~~~~~~~~~~~~~~
365365

366366
This code example is a complete, standalone file that performs an ordered bulk
367-
write operation. It uses the on the ``movies`` collection in the
367+
write operation. It uses the ``movies`` collection in the
368368
``sample_mflix`` database included in the :atlas:`sample datasets
369369
</sample-data?jmp=docs_driver_java>` provided by Atlas. You can load them into
370370
your database on the free tier of MongoDB Atlas by following the :atlas:`Get
371371
Started with Atlas Guide </getting-started/#atlas-getting-started?jmp=docs_driver_java>`
372372

373-
.. include:: /includes/sample-data-note.rst
374-
375373
This example call to ``bulkWrite()`` includes examples of the ``InsertOneModel``,
376374
``UpdateOneModel``, ``DeleteOneModel``, and ``ReplaceOneModel``.
377375

378376
.. include:: /includes/connect-guide-note.rst
379377

380378
.. io-code-block::
381379

382-
.. input:: /includes/usage-examples/code-snippets/BulkWrite.java
380+
.. input:: /includes/crud/BulkWrite.java
383381
:language: java
384382
:dedent:
385383

source/crud/write-operations/delete.txt

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.. _java-fundamentals-delete:
2+
.. _java-usage-deletemany:
23

34
================
45
Delete Documents
@@ -147,14 +148,54 @@ collection:
147148
{ "_id": 1, "color": "red", "qty": 5 }
148149
{ "_id": 8, "color": "black", "qty": 8 }
149150

151+
Delete Example File
152+
-------------------
153+
154+
This code example is a complete, standalone file that performs a delete one
155+
operation and a delete many operation. It uses the ``movies`` collection in the
156+
``sample_mflix`` database included in the :atlas:`sample datasets
157+
</sample-data?jmp=docs_driver_java>` provided by Atlas. You can load them into
158+
your database on the free tier of MongoDB Atlas by following the :atlas:`Get
159+
Started with Atlas Guide </getting-started/#atlas-getting-started?jmp=docs_driver_java>`
160+
161+
This example used the ``eq()`` and ``lt()`` filters to query documents. For more
162+
information about filters, see the `Filters Class
163+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Filters.html>`__
164+
API documentation.
165+
166+
.. include:: /includes/connect-guide-note.rst
167+
168+
.. io-code-block::
169+
170+
.. input:: /includes/crud/Delete.java
171+
:language: java
172+
:dedent:
173+
174+
.. output::
175+
:language: none
176+
:visible: false
177+
178+
DeleteOne document count: 1
179+
DeleteMany document count: 4
180+
181+
More Information
182+
----------------
183+
150184
For more information about the methods and classes mentioned in this guide,
151185
see the following resources:
152186

153-
- `deleteOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#deleteOne(org.bson.conversions.Bson)>`__ API Documentation
154-
- `deleteMany() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#deleteMany(org.bson.conversions.Bson)>`__ API Documentation
155-
- `findOneAndDelete() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#findOneAndDelete(org.bson.conversions.Bson)>`__ API Documentation
156-
- `DeleteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteOptions.html>`__ API Documentation
157-
- `FindOneAndDeleteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/FindOneAndDeleteOptions.html>`__ API Documentation
158-
- :manual:`db.collection.deleteOne() </reference/method/db.collection.deleteMany/>` Server Manual Entry
159-
- :manual:`db.collection.deleteMany() </reference/method/db.collection.deleteOne/>` Server Manual Entry
160-
- :manual:`db.collection.findOneAndDelete() </reference/method/db.collection.findOneAndDelete/>` Server Manual Entry
187+
API Documentation
188+
~~~~~~~~~~~~~~~~~
189+
190+
- `deleteOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#deleteOne(org.bson.conversions.Bson)>`__
191+
- `deleteMany() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#deleteMany(org.bson.conversions.Bson)>`__
192+
- `findOneAndDelete() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#findOneAndDelete(org.bson.conversions.Bson)>`__
193+
- `DeleteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteOptions.html>`__
194+
- `FindOneAndDeleteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/FindOneAndDeleteOptions.html>`__
195+
196+
Server Manual Entries
197+
~~~~~~~~~~~~~~~~~~~~~
198+
199+
- :manual:`db.collection.deleteOne() </reference/method/db.collection.deleteMany/>`
200+
- :manual:`db.collection.deleteMany() </reference/method/db.collection.deleteOne/>`
201+
- :manual:`db.collection.findOneAndDelete() </reference/method/db.collection.findOneAndDelete/>`

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,31 @@ public static void main(String[] args) {
2525
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2626
MongoCollection<Document> collection = database.getCollection("movies");
2727

28-
Bson query = eq("title", "The Garbage Pail Kids Movie");
28+
Bson deleteOneQuery = eq("title", "The Garbage Pail Kids Movie");
2929

3030
try {
3131
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
32-
DeleteResult result = collection.deleteOne(query);
33-
System.out.println("Deleted document count: " + result.getDeletedCount());
32+
DeleteResult deleteOneResult = collection.deleteOne(deleteOneQuery);
33+
System.out.println("Deleted document count: " + deleteOneResult.getDeletedCount());
3434

3535
// Prints a message if any exceptions occur during the operation
3636
} catch (MongoException me) {
3737
System.err.println("Unable to delete due to an error: " + me);
3838
}
39+
40+
Bson deleteManyQuery = lt("imdb.rating", 1.9);
41+
42+
try {
43+
// Deletes all documents that have an "imdb.rating" value less than 1.9
44+
DeleteResult deleteManyResult = collection.deleteMany(deleteManyQuery);
45+
46+
// Prints the number of deleted documents
47+
System.out.println("Deleted document count: " + deleteManyResult.getDeletedCount());
48+
49+
// Prints a message if any exceptions occur during the operation
50+
} catch (MongoException me) {
51+
System.err.println("Unable to delete due to an error: " + me);
52+
}
3953
}
4054
}
4155
}

source/includes/usage-examples/code-snippets/DeleteMany.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

source/usage-examples/bulkWrite.txt

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)