Skip to content

Commit 88eb2b3

Browse files
DOCSP-15338 crud delete (#69)
* added CRUD Delete page
1 parent 5b4392f commit 88eb2b3

File tree

3 files changed

+261
-2
lines changed

3 files changed

+261
-2
lines changed

source/fundamentals/crud/write-operations.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ Write Operations
55
.. default-domain:: mongodb
66

77
- :doc:`/fundamentals/crud/write-operations/insert`
8+
- :doc:`/fundamentals/crud/write-operations/delete`
89
- :doc:`/fundamentals/crud/write-operations/upsert`
910

1011
..
11-
- :doc:`/fundamentals/crud/write-operations/delete`
1212
- :doc:`/fundamentals/crud/write-operations/change-a-document`
1313
- :doc:`/fundamentals/crud/write-operations/embedded-arrays`
1414

1515
.. toctree::
1616
:caption: Write Operations
1717

1818
/fundamentals/crud/write-operations/insert
19+
/fundamentals/crud/write-operations/delete
1920
/fundamentals/crud/write-operations/upsert
2021

2122
..
22-
/fundamentals/crud/write-operations/delete
2323
/fundamentals/crud/write-operations/change-a-document
2424
/fundamentals/crud/write-operations/embedded-arrays
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
=================
2+
Delete a Document
3+
=================
4+
5+
.. default-domain:: mongodb
6+
7+
Overview
8+
--------
9+
10+
You can remove documents by passing a query filter to the
11+
:java-docs:`deleteOne() </apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#deleteOne(org.bson.conversions.Bson)>`,
12+
:java-docs:`deleteMany() </apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#deleteMany(org.bson.conversions.Bson)>`
13+
or :java-docs:`findOneAndDelete() </apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#findOneAndDelete(org.bson.conversions.Bson)>` methods.
14+
15+
The ``deleteOne()`` method deletes a single document. If the query
16+
filter matches more than one document, the method will remove the first
17+
occurrence of a match in the collection.
18+
19+
The ``deleteMany()`` method deletes all documents that match the query
20+
filter.
21+
22+
The ``findOneAndDelete()`` method atomically finds and deletes the first
23+
occurrence of a match in the collection.
24+
25+
To specify a collation or hint an index, use
26+
:java-docs:`DeleteOptions </apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteOptions.html>`
27+
as a second parameter to the ``deleteOne()`` and ``deleteMany()`` methods.
28+
29+
To specify a collation, hint an index, specify sort order, or specify a
30+
projection on the returned document, use
31+
:java-docs:`FindOneAndDeleteOptions </apidocs/mongodb-driver-core/com/mongodb/client/model/FindOneAndDeleteOptions.html>`
32+
as the second parameter to the ``findOneAndDelete()`` method.
33+
34+
Examples
35+
--------
36+
37+
The following examples are about a paint store that sells eight different
38+
colors of paint. The store had their annual online sale resulting in the
39+
following documents in their ``paint_inventory`` collection:
40+
41+
.. code-block:: json
42+
43+
{ "_id": 1, "color": "red", "qty": 5 }
44+
{ "_id": 2, "color": "purple", "qty": 8 }
45+
{ "_id": 3, "color": "blue", "qty": 0 }
46+
{ "_id": 4, "color": "white", "qty": 0 }
47+
{ "_id": 5, "color": "yellow", "qty": 6 }
48+
{ "_id": 6, "color": "pink", "qty": 0 }
49+
{ "_id": 7, "color": "green", "qty": 0 }
50+
{ "_id": 8, "color": "black", "qty": 8 }
51+
52+
``deleteMany()``
53+
````````````````
54+
55+
The paint store website displays all documents in the
56+
``paint_inventory`` collection. To reduce customer confusion, the store
57+
wants to remove the colors that are out of stock.
58+
59+
To remove the out of stock colors, query the ``paint_inventory``
60+
collection where the ``qty`` is ``0`` and pass the query to the
61+
``deleteMany()`` method:
62+
63+
.. literalinclude:: /includes/fundamentals/code-snippets/Delete.java
64+
:language: java
65+
:dedent:
66+
:start-after: begin deleteManyExample
67+
:end-before: end deleteManyExample
68+
69+
The following shows the documents remaining in the ``paint_inventory``
70+
collection:
71+
72+
.. code-block:: json
73+
74+
{ "_id": 1, "color": "red", "qty": 5 }
75+
{ "_id": 2, "color": "purple", "qty": 8 }
76+
{ "_id": 5, "color": "yellow", "qty": 6 }
77+
{ "_id": 8, "color": "black", "qty": 8 }
78+
79+
``deleteOne()``
80+
```````````````
81+
82+
The store is donating the remaining quantity of their yellow paint. This
83+
means that the ``qty`` for yellow is now ``0`` and we need to remove yellow
84+
from the collection.
85+
86+
To remove yellow, query the ``paint_inventory`` collection where the
87+
``color`` is ``"yellow"`` and pass the query to the ``deleteOne()``
88+
method:
89+
90+
.. literalinclude:: /includes/fundamentals/code-snippets/Delete.java
91+
:language: java
92+
:dedent:
93+
:start-after: begin deleteOneExample
94+
:end-before: end deleteOneExample
95+
96+
The following shows the documents remaining in the ``paint_inventory``
97+
collection:
98+
99+
.. code-block:: json
100+
101+
{ "_id": 1, "color": "red", "qty": 5 }
102+
{ "_id": 2, "color": "purple", "qty": 8 }
103+
{ "_id": 8, "color": "black", "qty": 8 }
104+
105+
``findOneAndDelete()``
106+
``````````````````````
107+
108+
The store would like to raffle the remaining quantity of purple paint
109+
and remove purple from the ``paint_inventory`` collection.
110+
111+
To pick a color, query the ``paint_inventory`` collection where the
112+
``color`` is ``"purple"`` and pass the query to the ``findOneAndDelete()``
113+
method:
114+
115+
.. literalinclude:: /includes/fundamentals/code-snippets/Delete.java
116+
:language: java
117+
:dedent:
118+
:start-after: begin findOneAndDeleteExample
119+
:end-before: end findOneAndDeleteExample
120+
121+
Unlike the other delete methods, ``findOneAndDelete()`` returns the
122+
deleted document:
123+
124+
.. code-block:: json
125+
126+
{ "_id": 2, "color": "purple", "qty": 8 }
127+
128+
.. note::
129+
130+
If there are no matches to your query filter, no document gets
131+
deleted and the method returns null.
132+
133+
The following shows the documents remaining in the ``paint_inventory``
134+
collection:
135+
136+
.. code-block:: json
137+
138+
{ "_id": 1, "color": "red", "qty": 5 }
139+
{ "_id": 8, "color": "black", "qty": 8 }
140+
141+
See the following documentation for more information about the deleting
142+
a document:
143+
144+
- :manual:`db.collection.deleteOne() </reference/method/db.collection.deleteMany/>`
145+
- :manual:`db.collection.deleteMany() </reference/method/db.collection.deleteOne/>`
146+
- :manual:`db.collection.findOneAndDelete() </reference/method/db.collection.findOneAndDelete/>`
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package docs;
2+
3+
import com.mongodb.client.MongoClient;
4+
import com.mongodb.client.MongoClients;
5+
import com.mongodb.client.MongoCollection;
6+
import com.mongodb.client.MongoDatabase;
7+
8+
import org.bson.Document;
9+
import org.bson.conversions.Bson;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
import com.mongodb.client.model.Filters;
15+
16+
public class Delete {
17+
18+
private final MongoCollection<Document> collection;
19+
private final MongoClient mongoClient;
20+
private final MongoDatabase database;
21+
22+
private Delete() {
23+
final String uri = System.getenv("DRIVER_REF_URI");
24+
25+
mongoClient = MongoClients.create(uri);
26+
database = mongoClient.getDatabase("crudOps");
27+
collection = database.getCollection("delete");
28+
}
29+
30+
public static void main(String [] args){
31+
Delete delete = new Delete();
32+
delete.preview(true);
33+
delete.setupPaintCollection();
34+
35+
System.out.println("Delete Many:");
36+
delete.deleteManyExample();
37+
delete.preview(false);
38+
39+
System.out.println("Delete One:");
40+
delete.deleteOneExample();
41+
delete.preview(false);
42+
43+
// System.out.println("Deleted:");
44+
// delete.findOneAndDeleteNullExample();
45+
// System.out.println("Find One and Delete:");
46+
// delete.preview(true);
47+
48+
System.out.println("Deleted:");
49+
delete.findOneAndDeleteExample();
50+
System.out.println("Find One and Delete:");
51+
delete.preview(true);
52+
}
53+
54+
private void deleteManyExample(){
55+
// begin deleteManyExample
56+
Bson filter = Filters.eq("qty", 0);
57+
collection.deleteMany(filter);
58+
// end deleteManyExample
59+
}
60+
61+
private void findOneAndDeleteExample(){
62+
// begin findOneAndDeleteExample
63+
Bson filter = Filters.eq("qty", 8);
64+
System.out.println(collection.findOneAndDelete(filter).toJson());
65+
// end findOneAndDeleteExample
66+
}
67+
68+
private void findOneAndDeleteNullExample(){
69+
// begin findOneAndDeleteNullExample
70+
Bson filter = Filters.eq("qty", 1);
71+
System.out.println(collection.findOneAndDelete(filter));
72+
// end findOneAndDeleteNullExample
73+
}
74+
75+
private void deleteOneExample(){
76+
// begin deleteOneExample
77+
Bson filter = Filters.eq("color", "yellow");
78+
collection.deleteOne(filter);
79+
// end deleteOneExample
80+
}
81+
private void preview(boolean drop){
82+
Bson filter = Filters.empty();
83+
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
84+
if (drop){
85+
collection.drop();
86+
}
87+
}
88+
89+
private void setupPaintCollection() {
90+
91+
List<Document> deletedata = new ArrayList<>();
92+
93+
Document p1 = new Document("_id", 1).append("color", "red").append("qty", 5);
94+
Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 8);
95+
Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 0);
96+
Document p4 = new Document("_id", 4).append("color", "white").append("qty", 0);
97+
Document p5 = new Document("_id", 5).append("color", "yellow").append("qty", 6);
98+
Document p6 = new Document("_id", 6).append("color", "pink").append("qty", 0);
99+
Document p7 = new Document("_id", 7).append("color", "green").append("qty", 0);
100+
Document p8 = new Document("_id", 8).append("color", "black").append("qty", 8);
101+
102+
deletedata.add(p1);
103+
deletedata.add(p2);
104+
deletedata.add(p3);
105+
deletedata.add(p4);
106+
deletedata.add(p5);
107+
deletedata.add(p6);
108+
deletedata.add(p7);
109+
deletedata.add(p8);
110+
111+
collection.insertMany(deletedata);
112+
}
113+
}

0 commit comments

Comments
 (0)