本例以Windows, JDK1.8, MongoDB v3.4.6 为基础,演示使用Java 对 MongoDB 数据库的常用的增删查改操作。
用到的技术和工具
- Java 8
- MongoDB v3.4.6
- Maven 3.10
- IntelliJ IDEA
Maven pom.xml
本例以Maven创建项目,所以在pom.xml中需要添加对应的依赖jar包.
添加MongoDB驱动包:
1 2 3 4 5 6 7
| <dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> <version>3.4.0-beta1</version> </dependency> </dependencies>
|
Mongo Connection 获取连接
1 2 3 4 5
|
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://localhost:27001"); MongoClient mongoClient = new MongoClient(mongoClientURI);
|
Access Database 获取数据库
获取”test”数据库:
1 2
| MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
|
Access Collection
获取”test” Collection:
1 2
| MongoCollection<Document> collection = mongoDatabase.getCollection("test");
|
Save example 保存数据
包括一条数据和一次性保存多条数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
Document document = new Document(); document.append("name", "Jack Chen"); document.append("age", 22);
collection.insertOne(document);
List<Document> documents = new ArrayList<Document>();
documents.add(new Document("name", "Jhone Du").append("age", 20)); documents.add(new Document("name", "Tom Wang").append("age", 30));
collection.insertMany(documents);
|
Find example 查询数据
根据条件查询和一次性查询出全部的数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Document document1 = collection.find(eq("name", "Jack Chen")).first(); System.out.println(document1.toJson());
MongoCursor<Document> cursor = collection.find().iterator(); while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); }
for (Document cur : collection.find()) { System.out.println(cur.toJson()); }
|
Update example 更新数据
根据条件更新一条数据:
1 2
| collection.updateOne(eq("name", "Tom Wang"), new Document("$set", new Document("name", "Tom Cheng")));
|
Delete example 删除数据
根据条件删除一条数据:
1 2
| collection.deleteOne(eq("name", "Jhone Du"));
|
Hello Wolrd 完整实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package com.devnp;
import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import org.bson.Document;
import java.util.ArrayList; import java.util.List;
import static com.mongodb.client.model.Filters.eq;
public class MongoDBHelloWorld {
public static void main(String [] args){
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://localhost:27001"); MongoClient mongoClient = new MongoClient(mongoClientURI);
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
Document document = new Document(); document.append("name", "Jack Chen"); document.append("age", 22);
collection.insertOne(document);
List<Document> documents = new ArrayList<Document>();
documents.add(new Document("name", "Jhone Du").append("age", 20)); documents.add(new Document("name", "Tom Wang").append("age", 30));
collection.insertMany(documents);
Document document1 = collection.find(eq("name", "Jack Chen")).first(); System.out.println(document1.toJson());
MongoCursor<Document> cursor = collection.find().iterator(); while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); }
for (Document cur : collection.find()) { System.out.println(cur.toJson()); }
collection.updateOne(eq("name", "Tom Wang"), new Document("$set", new Document("name", "Tom Cheng")));
collection.deleteOne(eq("name", "Jhone Du"));
} }
|
Code 代码下载
java-mongodb-quick.zip
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE