Skip to content

Commit f37bea4

Browse files
committed
insert
1 parent 9dadaf1 commit f37bea4

File tree

9 files changed

+70
-174
lines changed

9 files changed

+70
-174
lines changed

source/crud/write-operations/bulk.txt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -360,21 +360,17 @@ see the following API documentation:
360360
- `ordered() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/BulkWriteOptions.html#ordered(boolean)>`__
361361

362362

363-
BulkWrite Example File
364-
~~~~~~~~~~~~~~~~~~~~~~
363+
BulkWrite Example
364+
~~~~~~~~~~~~~~~~~
365+
366+
The following code is a complete, standalone file that performs an ordered bulk
367+
write operation.
365368

366-
This code example is a complete, standalone file that performs an ordered bulk
367-
write operation. It uses the ``movies`` collection in the
368-
``sample_mflix`` database included in the :atlas:`sample datasets
369-
</sample-data?jmp=docs_driver_java>` provided by Atlas. You can load them into
370-
your database on the free tier of MongoDB Atlas by following the :atlas:`Get
371-
Started with Atlas Guide </getting-started/#atlas-getting-started?jmp=docs_driver_java>`
369+
.. include:: /includes/crud/example-intro.rst
372370

373371
This example call to ``bulkWrite()`` includes examples of the ``InsertOneModel``,
374372
``UpdateOneModel``, ``DeleteOneModel``, and ``ReplaceOneModel``.
375373

376-
.. include:: /includes/connect-guide-note.rst
377-
378374
.. io-code-block::
379375

380376
.. input:: /includes/crud/BulkWrite.java

source/crud/write-operations/delete.txt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,19 @@ collection:
148148
{ "_id": 1, "color": "red", "qty": 5 }
149149
{ "_id": 8, "color": "black", "qty": 8 }
150150

151-
Delete Example File
152-
-------------------
151+
Delete Example
152+
--------------
153153

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>`
154+
The following code is a complete, standalone file that performs a delete one
155+
operation and a delete many operation.
160156

161-
This example used the ``eq()`` and ``lt()`` filters to query documents. For more
157+
.. include:: /includes/crud/example-intro.rst
158+
159+
The query uses the ``eq()`` and ``lt()`` filters to query documents. For more
162160
information about filters, see the `Filters Class
163161
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Filters.html>`__
164162
API documentation.
165163

166-
.. include:: /includes/connect-guide-note.rst
167-
168164
.. io-code-block::
169165

170166
.. input:: /includes/crud/Delete.java

source/crud/write-operations/insert.txt

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,37 @@ The output of the preceding code resembles the following:
159159
.. code-block::
160160
:copyable: false
161161

162-
Inserted documents with the following ids: [60930c3aa982931c20ef6cd7, 60930c3aa982931c20ef6cd8]
162+
Inserted documents with the following ids: [60930c3aa982931c20ef6cd7,
163+
60930c3aa982931c20ef6cd8]
163164

164-
For more information about the methods and classes mentioned in this section,
165-
see the following resources:
166165

167-
- `insertMany() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#insertMany(java.util.List)>`__ API Documentation
168-
- `InsertManyResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/InsertManyResult.html>`__ API Documentation
169-
- Manual Explanation on :manual:`insertMany() </reference/method/db.collection.insertMany/>`
170-
- Runnable :ref:`Insert Multiple Documents Example <java-usage-insertmany>`
166+
Insert Example
167+
--------------
168+
169+
The following code is a complete, standalone file that performs an insert one
170+
operation and an insert many operation.
171+
172+
.. include:: /includes/crud/example-intro.rst
173+
174+
.. io-code-block::
171175

172-
Summary
173-
-------
176+
.. input:: /includes/crud/Insert.java
177+
:language: java
178+
:dedent:
174179

175-
There are three ways to perform an insert operation, but we focused on two:
180+
.. output::
181+
:language: none
182+
:visible: false
176183

177-
- The ``insertOne()`` method inserts a single document.
178-
- The ``insertMany()`` method inserts multiple documents.
184+
InsertOne document id: BsonObjectId{value=...}
185+
InsertMany document ids: {0=BsonObjectId{value=...}, 1=BsonObjectId{value=...}}
179186

180-
Both methods automatically generate an ``_id`` if you omit the field in
181-
your document.
187+
More Information
188+
----------------
189+
190+
For more information about the methods and classes mentioned in this section,
191+
see the following resources:
182192

183-
If the insertion is successful, both methods return an instance
184-
representing the ``_id`` of each new document.
193+
- `insertMany() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#insertMany(java.util.List)>`__ API Documentation
194+
- `InsertManyResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/InsertManyResult.html>`__ API Documentation
195+
- Manual Explanation on :manual:`insertMany() </reference/method/db.collection.insertMany/>`

source/includes/crud/Delete.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void main(String[] args) {
3030
try {
3131
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
3232
DeleteResult deleteOneResult = collection.deleteOne(deleteOneQuery);
33-
System.out.println("Deleted document count: " + deleteOneResult.getDeletedCount());
33+
System.out.println("DeleteOne document count: " + deleteOneResult.getDeletedCount());
3434

3535
// Prints a message if any exceptions occur during the operation
3636
} catch (MongoException me) {
@@ -44,7 +44,7 @@ public static void main(String[] args) {
4444
DeleteResult deleteManyResult = collection.deleteMany(deleteManyQuery);
4545

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

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

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.mongodb.client.MongoCollection;
1414
import com.mongodb.client.MongoDatabase;
1515
import com.mongodb.client.result.InsertOneResult;
16+
import com.mongodb.client.result.InsertManyResult;
1617

1718
public class InsertOne {
1819
public static void main(String[] args) {
@@ -25,13 +26,30 @@ public static void main(String[] args) {
2526

2627
try {
2728
// Inserts a sample document describing a movie into the collection
28-
InsertOneResult result = collection.insertOne(new Document()
29+
InsertOneResult iOResult = collection.insertOne(new Document()
2930
.append("_id", new ObjectId())
3031
.append("title", "Ski Bloopers")
3132
.append("genres", Arrays.asList("Documentary", "Comedy")));
3233

3334
// Prints the ID of the inserted document
34-
System.out.println("Success! Inserted document id: " + result.getInsertedId());
35+
System.out.println("InsertOne document id: " + result.getInsertedId());
36+
37+
// Prints a message if any exceptions occur during the operation
38+
} catch (MongoException me) {
39+
System.err.println("Unable to insert due to an error: " + me);
40+
}
41+
42+
// Creates two sample documents containing a "title" field
43+
List<Document> movieList = Arrays.asList(
44+
new Document().append("title", "Short Circuit 3"),
45+
new Document().append("title", "The Lego Frozen Movie"));
46+
47+
try {
48+
// Inserts sample documents describing movies into the collection
49+
InsertManyResult result = collection.insertMany(movieList);
50+
51+
// Prints the IDs of the inserted documents
52+
System.out.println("InsertMany document ids: " + result.getInsertedIds());
3553

3654
// Prints a message if any exceptions occur during the operation
3755
} catch (MongoException me) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This example connects to an instance of MongoDB using a connection URI. To learn
2+
more about connecting to your MongoDB instance, see the :ref:`connection guide
3+
<connect-to-mongodb>`. It also uses the ``movies`` collection in the
4+
``sample_mflix`` database included in the :atlas:`sample datasets
5+
</sample-data?jmp=docs_driver_java>` provided by Atlas. You can load them into
6+
your database on the free tier of MongoDB Atlas by following the :atlas:`Get
7+
Started with Atlas Guide
8+
</getting-started/#atlas-getting-started?jmp=docs_driver_java>`.

source/usage-examples/delete-operations.txt

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

source/usage-examples/deleteMany.txt

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

source/usage-examples/deleteOne.txt

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

0 commit comments

Comments
 (0)