本文共 3368 字,大约阅读时间需要 11 分钟。
环境配置:在 Java 程序中如果要使用 MongoDB,你需要确保已经安装了 Java 环境及 MongoDB JDBC 驱动。
首先你必须下载mongo jar包,下载地址:, 请确保下载最新版本。
https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/
Hello MongoDB -- Java编程DEMO:
package com.xctdemo.main;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;import com.mongodb.client.model.Filters;public class HelloMongoDB { public static void main(String[] args) { System.out.println("---- Hello MongoDB ----"); try { // 连接到 mongodb 服务 MongoClient mongoClient = new MongoClient("localhost", 27017); // 连接到数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("world"); System.out.println("Connect to database successfully"); /* * mongoDatabase.createCollection("city"); * System.out.println("集合city创建成功"); // */ // 获取集合 MongoCollectioncollection = mongoDatabase .getCollection("city"); System.out.println("集合 city 选择成功"); // 在集合中插入文档 /** * 1. 创建文档 org.bson.Document 参数为key-value的格式 2. 创建文档集合List * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List ) * 插入单个文档可以用 mongoCollection.insertOne(Document) * */ Document document = new Document("name", "beijing") .append("country", "china").append("provice", "BEIJING") .append("people-count", 1000); List documents = new ArrayList (); documents.add(document); collection.insertMany(documents); System.out.println("文档插入成功-Many"); Document mydocument = new Document("name", "washington") .append("country", "america") .append("provice", "WASHINGTON") .append("people-count", 1200); collection.insertOne(mydocument); System.out.println("文档插入成功-One"); // 检索所有文档 showDocuments(collection); // 删除符合条件的第一个文档 // collection.deleteOne(Filters.eq("name", "beijing")); // 删除所有符合条件的文档 collection.deleteMany(Filters.eq("name", "beijing")); System.out.println("---- deleted OK ----"); System.out.println("--------------------------"); // 更新文档 将文档中likes=100的文档修改为likes=200 collection.updateMany(Filters.eq("name", "washington"), new Document("$set", new Document("country", "JAPAN"))); // 检索所有文档 showDocuments(collection); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } System.out.println("---- over ----"); } private static void showDocuments(MongoCollection collection) { /** * 1. 获取迭代器FindIterable 2. 获取游标MongoCursor 3. * 通过游标遍历检索出的文档集合 * */ FindIterable findIterable = collection.find(); MongoCursor mongoCursor = findIterable.iterator(); while (mongoCursor.hasNext()) { System.out.println(mongoCursor.next()); // .get("name") } }}
转载地址:http://etfyl.baihongyu.com/