HiSEN

IDEA maven项目通过mongo-java-driver折腾一下MongoDB

这几天折腾ubuntu然后安装了下mongodb

通过Oracle VM VirtualBox端口转发,连接了虚拟机的MongoDB

1.用idea创建一个maven项目

2.在pom.xml中添加mongodb java驱动

1
2
3
4
5
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.2.2</version>
</dependency>

3.参考官方:MongoDB Driver Quick Tour

本用例github地址:mongodbTest

贴下代码:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.hisen.jdbc;

import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

import static com.mongodb.client.model.Accumulators.sum;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Sorts.descending;
import static com.mongodb.client.model.Updates.inc;
import static com.mongodb.client.model.Updates.set;
import static java.util.Collections.singletonList;

/**
* Created by hisenyuan on 2017/2/20 at 13:42.
*/
public class QuickTour {
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("mydb");
//gets the collection test
MongoCollection<Document> collection = database.getCollection("test");
//插入数据
//insert(collection);
//插入大量数据
insertMany(collection);
//统计条数
count(collection);
//打印查询到的第一条数据
print(collection);
//打印所有数据
findAll(collection);
//按条件查询一条数据
findOneWithFilter(collection);
//按条件查询一个Set集合
getSet(collection);
//排序,输出第一条
sortDocuments(collection);
//获取指定的field
projectingFields(collection);
aggregations(collection);
//更新
update(collection);
//删除数据
delete(collection);
}

/**
* 插入单个文档
* @param collection
*/
public static void insert(MongoCollection<Document> collection) {
//create document
Document doc = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("info", new Document("x", 203).append("y", 102));
//insert the document into the collection
collection.insertOne(doc);
}

/**
* 一次性插入多条数据,用ArrayList拼装
* @param collection
*/
public static void insertMany(MongoCollection<Document> collection) {
//Create the documents in a loop
List<Document> documents = new ArrayList<Document>();
for (int i = 0; i < 100; i++) {
documents.add(new Document("i", i));
}
collection.insertMany(documents);
}

/**
* 统计集合中的总条数
* @param collection
*/
public static void count(MongoCollection<Document> collection) {
//Count Documents in A Collection
System.out.println("总条数:" + collection.count());
}

/**
* 打印查询到的第一条数据
* @param collection
*/
public static void print(MongoCollection<Document> collection) {
//prints the first document found in the collection
Document myDoc = collection.find().first();
System.out.println(myDoc.toJson());
}

/**
* 查询集合中所有的数据
* 不推荐使用foreach循环
* @param collection
*/
public static void findAll(MongoCollection<Document> collection) {
System.out.println("----------------输出所有数据----------------");
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
}

/**
* 条件查询单条数据
* @param collection
*/
public static void findOneWithFilter(MongoCollection<Document> collection) {
//Get A Single Document with a Query Filter
Document myDoc = collection.find(eq("i", 71)).first();
System.out.println(myDoc.toJson());
}

/**
* 获得一个Set集合的数据
* @param collection
*/
public static void getSet(MongoCollection<Document> collection) {
// now use a range query to get a larger subset
Block<Document> printBlock = new Block<Document>() {
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
//i的值大于98的数据
System.out.println("----------------i大于98的数据----------------");
collection.find(gt("i", 98)).forEach(printBlock);
//50 - 51 gt:大于 lte:小于等于
System.out.println("----------------50<i<=51的数据----------------");
collection.find(and(gt("i", 50), lte("i", 51))).forEach(printBlock);
}

/**
* 排序
* @param collection
*/
public static void sortDocuments(MongoCollection<Document> collection) {
//根据i的值进行排序
Document myDoc = collection.find(exists("i")).sort(descending("i")).first();
System.out.println("----------------排序,输出第一条----------------");
System.out.println(myDoc.toJson());
}

/**
* 获取想要的field(字段)
* @param collection
*/
public static void projectingFields(MongoCollection<Document> collection) {
System.out.println("----------------获取指定field,输出第一条----------------");
//排除ID字段
Document myDoc = collection.find().projection(excludeId()).first();
System.out.println(myDoc.toJson());
}

public static void aggregations(MongoCollection<Document> collection) {
/*
collection.aggregate(asList(
match(gt("i", 0)),
project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}")))
).forEach(printBlock);
*/
//求和
Document myDoc = collection.aggregate(singletonList(group(null, sum("total", "$i")))).first();
System.out.println(myDoc.toJson());
}

/**
* 更新document的方法
* @param collection
*/
public static void update(MongoCollection<Document> collection) {
//更新一个,如果不引入下面这个包,set方法会报错
//import static com.mongodb.client.model.Updates.*;
collection.updateOne(eq("i", 10), set("i", 110));
//更新多个,小于100的都加100
UpdateResult updateResult = collection.updateMany(lt("i", 190), inc("i", 100));
System.out.println("----------------更新条数----------------");
System.out.println(updateResult.getModifiedCount());
}

/**
* 删除数据的方法
* @param collection
*/
public static void delete(MongoCollection<Document> collection) {
collection.deleteOne(eq("i", 210));
//gte 大于等于100的都删除
DeleteResult deleteResult = collection.deleteMany(gte("i", 100));
System.out.println("----------------删除条数----------------");
System.out.println(deleteResult.getDeletedCount());
}
/*
public static void bulk(MongoCollection<Document> collection) {
// 2. Ordered bulk operation - order is guarenteed
collection.bulkWrite(
Arrays.asList(new InsertOneModel<>(new Document("_id", 4)),
new InsertOneModel<>(new Document("_id", 5)),
new InsertOneModel<>(new Document("_id", 6)),
new UpdateOneModel<>(new Document("_id", 1),
new Document("$set", new Document("x", 2))),
new DeleteOneModel<>(new Document("_id", 2)),
new ReplaceOneModel<>(new Document("_id", 3),
new Document("_id", 3).append("x", 4))));
// 2. Unordered bulk operation - no guarantee of order of operation
collection.bulkWrite(
Arrays.asList(new InsertOneModel<>(new Document("_id", 4)),
new InsertOneModel<>(new Document("_id", 5)),
new InsertOneModel<>(new Document("_id", 6)),
new UpdateOneModel<>(new Document("_id", 1),
new Document("$set", new Document("x", 2))),
new DeleteOneModel<>(new Document("_id", 2)),
new ReplaceOneModel<>(new Document("_id", 3),
new Document("_id", 3).append("x", 4))),
new BulkWriteOptions().ordered(false));
}
*/
}