Skip to content

Commit 21e8c2e

Browse files
authored
Update 2023-10-23-inserting-documents-in-mongodb-in-java.md
1 parent 242a0d4 commit 21e8c2e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

‎_posts/mongodb/mongodb-crud-operations/2023-10-23-inserting-documents-in-mongodb-in-java.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: "In this tutorial, we will see how to insert a document in MongoDB
99

1010
In this tutorial, we will see how to insert a document in MongoDB in Java applications.
1111

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)
1313

1414
```java
1515
String connectionString = "mongodb+srv://user123:password123@cluster0.example.mongodb.net/?retryWrites=true&w=majority";
@@ -20,17 +20,17 @@ The above `connectionString` is just an example, kindly get your valid connectio
2020

2121
We can insert a single or multiple documents in MongoDB Collections.
2222

23-
Let's see how to insert single document in MongoDB.
23+
Let's see how to insert a single document in MongoDB.
2424

25-
## Insert a Single Document in MongoDB Collection
25+
## Insert a Single Document in the MongoDB Collection
2626

2727
We can use the `insertOne()` method to insert a single document.
2828

29-
The `insertOne()` method accepts an Object that contains Document data and returns `InsertOneResult` Object.
29+
The `insertOne()` method accepts an Object that contains Document data and returns an `InsertOneResult` Object.
3030

31-
We can get id of the inserted document from `InsertOneResult` (like `insertOneResult.getInsertedId()`.
31+
We can get the id of the inserted document from `InsertOneResult` (like `insertOneResult.getInsertedId()`.
3232

33-
**Java Code to Insert a Single Document in a collection**
33+
**Java Code to Insert a Single Document in a Collection**
3434

3535
```java
3636
MongoDatabase database = mongoClient.getDatabase("apple_corp");
@@ -49,15 +49,15 @@ BsonValue id = insertOneResult.getInsertedId();
4949
System.out.println(id);
5050
```
5151

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.
5353

5454
## Insert Multiple Documents in MongoDB Collection
5555

56-
We can use the `insertMany()` method to insert multiple document.
56+
We can use the `insertMany()` method to insert multiple documents.
5757

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.
5959

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()))`.
6161

6262
**Java Code To Insert Multiple Documents in Collection**
6363

@@ -74,4 +74,4 @@ InsertManyResult insertManyResult = collection.insertMany(students);
7474
insertManyResult.getInsertedIds().forEach((x,y)-> System.out.println(y.asObjectId()));
7575
```
7676

77-
Above code will insert the `student1` and `student2` documents in the `students` collection. Also, it will print ids of inserted documents.
77+
The above code will insert the `student1` and `student2` documents in the `students` collection. Also, it will print the ids of inserted documents.

0 commit comments

Comments
 (0)