(1)连接
连接字符串
(2)CRUD
类与接口
解析
(3)maven管理方法
org.mongodb
mongodb-driver-legacy
4.10.2
package org.example;
import com.mongodb.client.*;
import org.bson.Document;
import java.util.Arrays;
/**
* @author gzb Email:[email protected]
* @since 2023/11/06 14:27
*/
public class t1 {
public static void main(String[] args) {
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase mongoDatabase = mongoClient.getDatabase("myDB");
MongoCollection collection = mongoDatabase.getCollection("video");
System.out.println("before operation");
insertExample(collection);
System.out.println("after operation");
FindIterable documents = collection.find();
for (Document document: documents){
System.out.println(document.toJson());
}
mongoClient.close(); // 记得关闭客户端连接
}
private static void insertExample(MongoCollection collection) {
collection.insertMany(Arrays.asList(
new Document("title", "MongoDB installation")
.append("author", "Mike")
.append("tags", Arrays.asList("MongoDB", "database"))
.append("views", 1221)
.append("evaluation", new Document("like", 22).append("forward", 10)),
new Document("title", "Java programming ")
.append("author", "Mike")
.append("tags", Arrays.asList("Java", "course"))
.append("views", 3411)
.append("evaluation", new Document("like", 112).append("forward", 32)),
new Document("title", "Music recommend")
.append("author", "Joe")
.append("tags", Arrays.asList("music", "pop"))
.append("views", 38)
.append("evaluation", new Document("like", 2))
));
}
}
private static void insertExample(MongoCollection collection) {
collection.insertMany(Arrays.asList(
new Document("title", "MongoDB installation")
.append("author", "Mike")
.append("tags", Arrays.asList("MongoDB", "database"))
.append("views", 1221)
.append("evaluation", new Document("like", 22).append("forward", 10)),
new Document("title", "Java programming ")
.append("author", "Mike")
.append("tags", Arrays.asList("Java", "course"))
.append("views", 3411)
.append("evaluation", new Document("like", 112).append("forward", 32)),
new Document("title", "Music recommend")
.append("author", "Joe")
.append("tags", Arrays.asList("music", "pop"))
.append("views", 38)
.append("evaluation", new Document("like", 2))
));
}
private static void delete(MongoCollection collection){
collection.deleteMany(Filters.eq("author", "Joe"));
}
private static void update(MongoCollection collection) {
collection.updateOne(
new Document("title", "MongoDB installation").append("author", "Mike"),
Updates.inc("views", 1));
}
还有set,pop,push等操作
private static void replace(MongoCollection collection) {
collection.replaceOne(
Filters.eq("author", "tom"),
new Document("author", "jacky").append("count",2)
);
}
private static void select(MongoCollection collection) {
Bson filter = Filters.gt("evaluation.like", 10);
FindIterable documents = collection.find(filter);
for (Document document : documents) {
System.out.println(document.toJson());
}
}
MongoCursor mongoCursor = collection.aggregate(
Arrays.asList(
Aggregates.match(Filters.empty()),
Aggregates.group("$author", Accumulators.sum("count", 1))
)).cursor();