本例以Windows, JDK1.8, MongoDB v3.4.6 为基础,演示使用JavaMongoDB 数据库的常用的增删查改操作。

用到的技术和工具

  1. Java 8
  2. MongoDB v3.4.6
  3. Maven 3.10
  4. 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
//create connection
//MongoClient mongoClient = new MongoClient("localhost", 27001); //old

MongoClientURI mongoClientURI = new MongoClientURI("mongodb://localhost:27001");
MongoClient mongoClient = new MongoClient(mongoClientURI);

Access Database 获取数据库

获取”test”数据库:

1
2
//access database
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");

Access Collection

获取”test” Collection:

1
2
//access collection
MongoCollection<Document> collection = mongoDatabase.getCollection("test");

Save example 保存数据

包括一条数据和一次性保存多条数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//save
//insert one
Document document = new Document();
document.append("name", "Jack Chen");
document.append("age", 22);

collection.insertOne(document);

//insert many
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
//select
//find one
Document document1 = collection.find(eq("name", "Jack Chen")).first();
System.out.println(document1.toJson());

//find all
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
//update
collection.updateOne(eq("name", "Tom Wang"), new Document("$set", new Document("name", "Tom Cheng")));

Delete example 删除数据

根据条件删除一条数据:

1
2
//delete
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;


/**
* Created by duliu on 31/7/2017.
*/
public class MongoDBHelloWorld {

public static void main(String [] args){
//create connection
//MongoClient mongoClient = new MongoClient("localhost", 27001); //old

MongoClientURI mongoClientURI = new MongoClientURI("mongodb://localhost:27001");
MongoClient mongoClient = new MongoClient(mongoClientURI);

//access database
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");

//access collection
MongoCollection<Document> collection = mongoDatabase.getCollection("test");

//save
//insert one
Document document = new Document();
document.append("name", "Jack Chen");
document.append("age", 22);

collection.insertOne(document);

//insert many
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);

//select
//find one
Document document1 = collection.find(eq("name", "Jack Chen")).first();
System.out.println(document1.toJson());

//find all
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
collection.updateOne(eq("name", "Tom Wang"), new Document("$set", new Document("name", "Tom Cheng")));

//delete
collection.deleteOne(eq("name", "Jhone Du"));

}
}

Code 代码下载

d-logojava-mongodb-quick.zip