Java & MongoDB Tutorial
Java & MongoDB Useful Resources
Selected Reading
- Java & MongoDB - Sorting Records
- Java & MongoDB - Limiting Records
- Java & MongoDB - Referenced Documents
- Java & MongoDB - Embedded Documents
- Java & MongoDB - Delete Document
- Java & MongoDB - Update Document
- Java & MongoDB - Select Document
- Java & MongoDB - Insert Document
- Java & MongoDB - Display Collections
- Java & MongoDB - Drop Collection
- Java & MongoDB - Create Collection
- Java & MongoDB - Drop Database
- Java & MongoDB - Show Databases
- Java & MongoDB - Connect Database
- Java & MongoDB - Environment Setup
- Java & MongoDB - Overview
- Java & MongoDB - Home
Java & MongoDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java & MongoDB - Insert Document
Java & MongoDB - Insert Document
To insert a document in a collection, you can use collection.insertOne() or collection.insertMany() methods to insert documents in the collection.
// insert a single document collection.insertOne(document); // insert multiple documents collection.insertMany(documents);
Example
Following is the code snippet to insert documents in a collection −
import java.util.ArrayList; import java.util.List; import org.bson.Document; import com.mongodb.cpent.MongoCpent; import com.mongodb.cpent.MongoCpents; import com.mongodb.cpent.MongoCollection; import com.mongodb.cpent.MongoDatabase; pubpc class Tester { pubpc static void main(String[] args) { // Creating a Mongo cpent MongoCpent mongoCpent = MongoCpents.create("mongodb://localhost:27017"); MongoDatabase database = mongoCpent.getDatabase("myDb"); // Get the collection MongoCollection<Document> collection = database.getCollection("sampleCollection"); Document document = new Document("First_Name", "Mahesh") .append("Last_Name", "Parashar") .append("Date_Of_Birth", "1990-08-21") .append("e_mail", "mahesh_parashar.123@gmail.com") .append("phone", "9034343345"); collection.insertOne(document); List<Document> documents = new ArrayList<>(); documents.add(new Document("First_Name", "Radhika") .append("Last_Name", "Sharma") .append("Date_Of_Birth", "1995-09-26") .append("e_mail", "radhika_sharma.123@gmail.com") .append("phone", "9000012345")); documents.add(new Document("First_Name", "Rachel") .append("Last_Name", "Christopher") .append("Date_Of_Birth", "1990-02-16") .append("e_mail", "Rachel_Christopher.123@gmail.com") .append("phone", "9000054321")); documents.add(new Document("First_Name", "Fathima") .append("Last_Name", "Sheik") .append("Date_Of_Birth", "1990-02-16") .append("e_mail", "Fathima_Sheik.123@gmail.com") .append("phone", "9000054321")); collection.insertMany(documents); System.out.println("Documents inserted."); } }
Now, let s compile and run the above program as shown below.
$javac Tester.java $java Tester
Output
On executing, the above program gives you the following output.
Documents inserted.Advertisements