You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/mongodb/mongodb-crud-operations/2023-10-23-inserting-documents-in-mongodb-in-java.md
+11-11
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ description: "In this tutorial, we will see how to insert a document in MongoDB
9
9
10
10
In this tutorial, we will see how to insert a document in MongoDB in Java applications.
11
11
12
-
To insert a document in MongoDB collection, first we should have a MongoClient. Let's see how to create one. Or You can read more at [How to Connect to a MongoDB Atlas Cluster in a Java Application](/connecting-to-mongodb-atlas-cluster-in-java-application)
12
+
To insert a document in the MongoDB collection, first, we should have a MongoClient. Let's see how to create one. Or You can read more at [How to Connect to a MongoDB Atlas Cluster in a Java Application](/connecting-to-mongodb-atlas-cluster-in-java-application)
@@ -49,15 +49,15 @@ BsonValue id = insertOneResult.getInsertedId();
49
49
System.out.println(id);
50
50
```
51
51
52
-
The above code will insert a single document `employee` in `employees`mongo collection. And It will print the inserted documents id on the console.
52
+
The above code will insert a single document `employee` in the `employees`Mongo collection. It will print the inserted document id on the console.
53
53
54
54
## Insert Multiple Documents in MongoDB Collection
55
55
56
-
We can use the `insertMany()` method to insert multiple document.
56
+
We can use the `insertMany()` method to insert multiple documents.
57
57
58
-
The `insertMany()` method accepts a list of Objects that contains Document data and returns `InsertManyResult` Object.
58
+
The `insertMany()` method accepts a list of Objects that contains Document data and returns the `InsertManyResult` Object.
59
59
60
-
We can get id of the inserted documents from `InsertManyResult` (like `insertManyResult.getInsertedIds().forEach((x,y)-> System.out.println(y.asObjectId()))`.
60
+
We can get the `id` of the inserted documents from `InsertManyResult` (like `insertManyResult.getInsertedIds().forEach((x,y)-> System.out.println(y.asObjectId()))`.
61
61
62
62
**Java Code To Insert Multiple Documents in Collection**
0 commit comments