zoukankan      html  css  js  c++  java
  • MongoDB最简单的入门教程之三 使用Java代码往MongoDB里插入数据

    前两篇教程我们介绍了如何搭建MongoDB的本地环境:

    MongoDB最简单的入门教程之一 环境搭建

    以及如何用nodejs读取MongoDB里的记录:

    MongoDB最简单的入门教程之二 使用nodejs访问MongoDB

    这篇教程我们会介绍如何使用Java代码来连接MongoDB。

    如果您是基于Maven进行依赖管理的Java项目,只需要在您的pom.xml里加入下面的依赖定义,

    <dependency>
    
    <groupId>org.mongodb</groupId>
    
    <artifactId>mongodb-driver</artifactId>
    
    <version>3.6.4</version>
    
    </dependency>
    

    然后使用命令行mvn clean install后,您的本地maven仓库里会多出三个和用Java连接MongoDB相关的库:

    • bson

    • mongodb-driver

    • mongodb-driver-core

    当然也可以手动逐一下载jar文件:https://mongodb.github.io/mongo-java-driver/

    本文使用的是这三个文件,将它们下载到本地,再加入Java项目的classpath里。

    Java代码如下:

    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<Document> collection) {
    		Document document = new Document("name", "dog");
    		List<Document> documents = new ArrayList<Document>();
    		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<Document> collection = mongoDatabase
    			.getCollection("person");
    			// insert(collection);
    			FindIterable<Document> findIterable = collection.find();
    			MongoCursor<Document> 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();
    		}
    	}
    }
    

    和教程二相比,上述代码的insert方法里还展示了如何用Java代码给MongoDB数据库里增加记录。

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

    执行Java应用,发现通过insert方法加到数据库的记录也能被顺利读出来。

    MongoDB最简单的入门教程之三 使用Java代码往MongoDB里插入数据

    MongoDB最简单的入门教程之三 使用Java代码往MongoDB里插入数据

    要获取更多Jerry的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

  • 相关阅读:
    【MySQL】学生成绩
    【MySQL】统计累计求和
    【MySQL】查询不在表中的数据
    【MySQL】排名函数
    【Python】数据处理分析,一些问题记录
    【Github】如何下载csv文件/win10如何修改txt文件为csv文件
    【Windows】github无法访问/hosts文件只能另存为txt
    【C/C++】拔河比赛/分组/招商银行
    【C/C++】金币
    【C/C++】旋转数组的最小数字/ 剑指offer
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/9820821.html
Copyright © 2011-2022 走看看