Skip to content

Commit 218137d

Browse files
committed
modify
1 parent f37bea4 commit 218137d

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

source/crud/write-operations/modify.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@ documents match.
163163
see :manual:`Unique Indexes </core/index-unique/>` in the
164164
{+mdb-server+} manual.
165165

166+
Update Example
167+
~~~~~~~~~~~~~~
168+
169+
The following code is a complete, standalone file that performs an update one
170+
operation and an update many operation.
171+
172+
.. include:: /includes/crud/example-intro.rst
173+
174+
.. io-code-block::
175+
176+
.. input:: /includes/crud/Insert.java
177+
:language: java
178+
:dedent:
179+
180+
.. output::
181+
:language: none
182+
:visible: false
183+
184+
InsertOne document id: BsonObjectId{value=...}
185+
InsertMany document ids: {0=BsonObjectId{value=...}, 1=BsonObjectId{value=...}}
186+
166187
.. _replace-operation:
167188

168189
Replace

source/includes/crud/Delete.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public static void main(String[] args) {
2929

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

3535
// Prints a message if any exceptions occur during the operation
3636
} catch (MongoException me) {
@@ -41,10 +41,10 @@ public static void main(String[] args) {
4141

4242
try {
4343
// Deletes all documents that have an "imdb.rating" value less than 1.9
44-
DeleteResult deleteManyResult = collection.deleteMany(deleteManyQuery);
44+
DeleteResult result = collection.deleteMany(deleteManyQuery);
4545

4646
// Prints the number of deleted documents
47-
System.out.println("DeleteMany document count: " + deleteManyResult.getDeletedCount());
47+
System.out.println("DeleteMany document count: " + result.getDeletedCount());
4848

4949
// Prints a message if any exceptions occur during the operation
5050
} catch (MongoException me) {

source/includes/crud/Insert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void main(String[] args) {
2626

2727
try {
2828
// Inserts a sample document describing a movie into the collection
29-
InsertOneResult iOResult = collection.insertOne(new Document()
29+
InsertOneResult result = collection.insertOne(new Document()
3030
.append("_id", new ObjectId())
3131
.append("title", "Ski Bloopers")
3232
.append("genres", Arrays.asList("Documentary", "Comedy")));

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

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

27-
Document query = new Document().append("title", "Cool Runnings 2");
27+
// Instructs the driver to insert a new document if none match the query
28+
UpdateOptions options = new UpdateOptions().upsert(true);
29+
30+
31+
Document updateOneQuery = new Document().append("title", "Cool Runnings 2");
2832

2933
// Creates instructions to update the values of three document fields
30-
Bson updates = Updates.combine(
34+
Bson updateOneUpdates = Updates.combine(
3135
Updates.set("runtime", 99),
3236
Updates.addToSet("genres", "Sports"),
3337
Updates.currentTimestamp("lastUpdated"));
3438

35-
// Instructs the driver to insert a new document if none match the query
36-
UpdateOptions options = new UpdateOptions().upsert(true);
37-
3839
try {
3940
// Updates the first document that has a "title" value of "Cool Runnings 2"
40-
UpdateResult result = collection.updateOne(query, updates, options);
41+
UpdateResult result = collection.updateOne(updateOneQuery, updateOneUpdates, options);
4142

4243
// Prints the number of updated documents and the upserted document ID, if an upsert was performed
4344
System.out.println("Modified document count: " + result.getModifiedCount());
@@ -47,6 +48,25 @@ public static void main(String[] args) {
4748
} catch (MongoException me) {
4849
System.err.println("Unable to update due to an error: " + me);
4950
}
51+
52+
Bson updateManyQuery = gt("num_mflix_comments", 50);
53+
54+
// Creates instructions to update the values of two document fields
55+
Bson updateManyUpdates = Updates.combine(
56+
Updates.addToSet("genres", "Frequently Discussed"),
57+
Updates.currentTimestamp("lastUpdated"));
58+
59+
try {
60+
// Updates documents that have a "num_mflix_comments" value over 50
61+
UpdateResult result = collection.updateMany(updateManyQuery, updateManyUpdates);
62+
63+
// Prints the number of updated documents
64+
System.out.println("Modified document count: " + result.getModifiedCount());
65+
66+
// Prints a message if any exceptions occur during the operation
67+
} catch (MongoException me) {
68+
System.err.println("Unable to update due to an error: " + me);
69+
}
5070
}
5171
}
5272
}

0 commit comments

Comments
 (0)