Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to insert data into MongoDB using Java code

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to use Java code to insert data into MongoDB. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

In the first two tutorials, we showed you how to build a local environment for MongoDB:

Use nodejs to access MongoDB

We will show you how to use Java code to connect to MongoDB.

If you are a Java project for dependency management based on Maven, you only need to add the following dependency definition to your pom.xml

Org.mongodbmongodb-driver3.6.4

Then, after using the command line mvn clean install, you will have three more libraries related to connecting to MongoDB with Java in your local maven repository:

Bson

Mongodb-driver

Mongodb-driver-core

Of course, you can also download the jar files one by one manually: https://mongodb.github.io/mongo-java-driver/

This article uses these three files, download them locally, and then add them to the classpath of the Java project.

The Java code is as follows:

Package mongoDB;import java.util.ArrayList;import java.util.List;import org.bson.Document;import com.mongodb.MongoClient;import com.mongodb.client.FindIterable;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase;public class MongoDBTest {private static void insert (MongoCollection collection) {Document document = new Document ("name", "dog"); List documents = new ArrayList (); documents.add (document) Collection.insertMany (documents);} public static void main (String args []) {MongoClient mongoClient = null; try {mongoClient = new MongoClient ("localhost", 27017); MongoDatabase mongoDatabase = mongoClient.getDatabase ("admin"); System.out.println ("Connect to database successfully"); MongoCollection collection = mongoDatabase .getCollection ("person") / / insert (collection); FindIterable findIterable = collection.find (); MongoCursor mongoCursor = findIterable.iterator (); while (mongoCursor.hasNext ()) {System.out.println (mongoCursor.next ()) }} catch (Exception e) {System.err.println (e.getClass () .getName () + ":" + e.getMessage ());} finally {mongoClient.close ();}

Compared with tutorial 2, the insert method of the above code also shows how to use Java code to add records to the MongoDB database.

Private static void insert (MongoCollection collection) {Document document = new Document ("name", "dog"); List documents = new ArrayList (); documents.add (document); collection.insertMany (documents);}

After executing the Java application, it is found that the records added to the database through the insert method can also be read out smoothly.

The third of the simplest introductory tutorials for MongoDB is to insert data into MongoDB using Java code

The above is how to use Java code to insert data into MongoDB. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report