
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to insert a document into a MongoDB collection using Java?
You can insert a document into an existing collection in MongoDB using the insert() method.
Syntax
db.coll.insert(doc)
Where,
db is the database.
coll is the collection (name) in which you want to insert the document
doc is the document you want to insert.
Example
> use myDatabase() switched to db myDatabase() > db.createCollection(sample) { "ok" : 1 } > db.sample.insert({name:"Ram", age:26, city:"Hyderabad"}) WriteResult({ "nInserted" : 1 })
Using Java program
In Java, you can insert a document into a collection using the insertOne() method of the com.mongodb.client.MongoCollection interface. This method accepts a document (object) representing the document you want to insert as a parameter.
Therefore to create a collection in MongoDB using Java program −
Make sure you have installed MongoDB in your system
Add the following dependency to its pom.xml file of your Java project.
Example
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.2</version> </dependency>
Create a MongoDB client by instantiating the MongoClient class.
Connect to a database using the getDatabase() method.
Prepare the document to be inserted.
Get the object of the collection into which you want to insert the document, using the getCollection() method.
Invoke the insertOne() method by passing the document (created above) as a parameter.
Example
import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.MongoClient; public class InsertingDocument { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Connecting to the database MongoDatabase database = mongo.getDatabase("myDatabase"); //Creating a collection database.createCollection("students"); //Preparing a document Document document = new Document(); document.append("name", "Ram"); document.append("age", 26); document.append("city", "Hyderabad"); //Inserting the document into the collection database.getCollection("students").insertOne(document); System.out.println("Document inserted successfully"); } }
Output
Document inserted successfully
- Related Articles
- How to insert multiple document into a MongoDB collection using Java?
- How to update an existing document in MongoDB collection using Java?
- How to insert new documents into a MongoDB collection in your database?
- How to delete a MongoDB document using Java?
- How to delete document from a collection in MongoDB using deleteOne() method?
- How to create a MongoDB collection using Java?
- How to drop a MongoDB Collection using Java?
- How to insert a document with date in MongoDB?
- MongoDB query to update a specific document from a collection
- How to sort the documents of a MongoDB collection using java?
- How to Update multiple documents in a MongoDB collection using Java?
- Retrieving the first document in a MongoDB collection?
- Add new field to every document in a MongoDB collection?
- How to insert DATE into a MySQL column value using Java?
- How to remove all documents from a collection except a single document in MongoDB?
