coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

How To Insert A Document in MongoDB Collection in Java Applications?

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

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

1
2
String connectionString = "mongodb+srv://user123:[email protected]/?retryWrites=true&w=majority";
MongoClient mongoClient = MongoClients.create(connectionString);

The above connectionString is just an example, kindly get your valid connection String.

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

Let’s see how to insert a single document in MongoDB.

Insert a Single Document in the MongoDB Collection

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

The insertOne() method accepts an Object that contains Document data and returns an InsertOneResult Object.

We can get the id of the inserted document from InsertOneResult (like insertOneResult.getInsertedId().

Java Code to Insert a Single Document in a Collection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
MongoDatabase database = mongoClient.getDatabase("apple_corp");
MongoCollection<Document> collection = database.getCollection("employees");

Document employee = new Document("_id", new ObjectId())
        .append("id", "2023-06-1256")
        .append("name", "Tee Wilson")
        .append("joining_date", Date.from(LocalDate.of(2023, 6, 20).atStartOfDay(ZoneId.systemDefault()).toInstant()))
        .append("status", "onboarding")
        .append("bu", "newyork")
        .append("address", new Document().append("city", "NEW YORK").append("zip", 10022).append("street", "PARK AVENUE").append("number", 25626));

InsertOneResult insertOneResult = collection.insertOne(inspection);
BsonValue id = insertOneResult.getInsertedId();
System.out.println(id);

The above code will insert a single document employee in the employees Mongo collection. It will print the inserted document id on the console.

Insert Multiple Documents in MongoDB Collection

We can use the insertMany() method to insert multiple documents.

The insertMany() method accepts a list of Objects that contains Document data and returns the InsertManyResult Object.

We can get the id of the inserted documents from InsertManyResult (like insertManyResult.getInsertedIds().forEach((x,y)-> System.out.println(y.asObjectId())).

Java Code To Insert Multiple Documents in Collection

1
2
3
4
5
6
7
8
9
10
MongoDatabase database = mongoClient.getDatabase("texas_school");
MongoCollection<Document> collection = database.getCollection("students");

Document student1 = new Document().append("name","jack sparrow").append("roll_number","202306NJ253").append("due",1256).append("status","checking");

Document student2 = new Document().append("name","mary nelson").append("roll_number","202306NJ073").append("due",25).append("status","ready");

List<Document> students = Arrays.asList(student1, student2);
InsertManyResult insertManyResult = collection.insertMany(students);
insertManyResult.getInsertedIds().forEach((x,y)-> System.out.println(y.asObjectId()));

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

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus