kandi background
Explore Kits

How to insert a document/data into a MongoDB collection using Java

by loke Updated: Jan 15, 2023

In Java, MongoClient is a class in the MongoDB-driver library for connecting to a MongoDB database and performing various operations on the data. It is the main entry point for the MongoDB Java driver and represents a pool of connections to a MongoDB server or a replica set. You can use the MongoClient class to perform various operations on the data, such as inserting, updating, and deleting documents, creating and dropping collections, and more. 


To insert data into a MongoDB collection using the MongoClient class in Java, you can use the insertOne() or insertMany() methods of the MongoCollection class. 

  • insertOne(): A single document can be added to a MongoDB collection using the insertOne() method of the MongoCollection class in the Java driver for MongoDB. It puts a Document object into the collection as an argument. 
  • insertMany(): Multiple documents can be added to a MongoDB collection using the insertMany() method of the MongoCollection class in the Java driver for MongoDB. It puts a list of Document objects into the collection as an input. 


Here is an example of how to insert a document into a MongoDB collection using Java;


Fig 1: Preview of the output that you will get on running this code from your IDE

Code


  1. To use this code snippet, install mongodb. You can use this link to install.
  2. Copy the code using the "Copy" button above, and paste it in a Java file in your IDE.
  3. Add dependent library or download the dependent jar and add in your IDE class path.
  4. If you want to change the Database and collection name, you can modify it in the code.
  5. Run the file to insert a document into mongodb.

I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for "mongodb insert document in java" in kandi. You can try any such use case!

Development Libraries


mongo-java-driverby mongodb

Java star image 2528 Version:3.12.11

License: Permissive (Apache-2.0)

The Official Java driver for MongoDB

Support
Quality
Security
License
Reuse

mongo-java-driverby mongodb

Java star image 2528 Version:3.12.11 License: Permissive (Apache-2.0)

The Official Java driver for MongoDB
Support
Quality
Security
License
Reuse

You can add the dependent library in your gradle or maven files. you can get the dependancy xml in above link

You can search for any dependent library on kandi like Mongo java driver

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in open-jdk-11.0.8.
  2. The solution is tested on Mongo java driver 3.12.11 version.
  3. The solution is tested on Mongodb 4.4.0 version.

Using this solution, we are able to download an file using the Mongo java driver library in Java with simple steps. This process also facilities an easy to use, hassle free method to create a hands-on working version of code which would help us to insert document into mongodb in Java.

Support

  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page.


See similar Kits and Libraries

Java
Runtime Evironment
Server