mongodb java 3.2_MongoDB-JAVA-Driver 3.2版本常用代碼全整理(3) - 聚合

MongoDB的3.x版本Java驅動相對2.x做了全新的設計,類庫和使用方法上有很大區別。例如用Document替換BasicDBObject、通過Builders類構建Bson替代直接輸入$命令等,本文整理了基於3.2版本的常用增刪改查操作的使用方法。為了避免冗長的篇幅,分為增刪改、查詢、聚合、地理索引等幾部分。

聚合用於統計文檔個數、求和、最大最小值、求平均值等,功能和函數名稱和SQL中的count、distinct、group等關鍵字非常類似,此外,還可以通過JavaScript編寫MapReduce實現復雜的計算(性能損耗也會非常嚴重)。

首先來看3.x驅動中的聚合方法的聲明:

AggregateIterable aggregate(List extends Bson> pipeline)

參數類型是一個Bson的列表,而參數名稱是pipeline,其構建方式正如其名,是以多個Bson建立起一條管道,前一個Bson的輸出將作為后一個Bson的輸入,例如:

mc.aggregate(Arrays.asList(match(eq("owner", "tom")), group("$author", sum("totalWords", "$words"))));

首先用$match查找出owner=tom的文檔,並將結果集傳遞給$group並對字數求和。

下面來看更多命令用法,用於演示的類的基本代碼如下

import static com.mongodb.client.model.Accumulators.*;

import static com.mongodb.client.model.Aggregates.*;

import static com.mongodb.client.model.Filters.eq;

import java.text.ParseException;

import java.util.Arrays;

import org.bson.Document;

import com.mongodb.Block;

import com.mongodb.MongoClient;

import com.mongodb.client.AggregateIterable;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

public class AggregatesExamples {

public static void main(String[] args) throws ParseException {

//根據實際環境修改ip和端口

MongoClient mongoClient = new MongoClient("localhost", 27017);

MongoDatabase database = mongoClient.getDatabase("lesson");

AggregatesExamples client = new AggregatesExamples(database);

client.show();

mongoClient.close();

}

private MongoDatabase database;

public AggregatesExamples(MongoDatabase database) {

this.database = database;

}

public void show() {

MongoCollection mc = database.getCollection("blog");

//每次執行前清空集合以方便重復運行

mc.drop();

//插入用於測試的文檔

Document doc1 = new Document("title", "good day").append("owner", "tom").append("words", 300)

.append("comments", Arrays.asList(new Document("author", "joe").append("score", 3).append("comment", "good"), new Document("author", "white").append("score", 1).append("comment", "oh no")));

Document doc2 = new Document("title", "good").append("owner", "john").append("words", 400)

.append("comments", Arrays.asList(new Document("author", "william").append("score", 4).append("comment", "good"), new Document("author", "white").append("score", 6).append("comment", "very good")));

Document doc3 = new Document("title", "good night").append("owner", "mike").append("words", 200)

.append("tag", Arrays.asList(1, 2, 3, 4));

Document doc4 = new Document("title", "happiness").append("owner", "tom").append("words", 1480)

.append("tag", Arrays.asList(2, 3, 4));

Document doc5 = new Document("title", "a good thing").append("owner", "tom").append("words", 180)

.append("tag", Arrays.asList(1, 2, 3, 4, 5));

mc.insertMany(Arrays.asList(doc1, doc2, doc3, doc4, doc5));

AggregateIterable iterable = mc.aggregate(Arrays.asList(match(eq("owner", "tom")),

group("$author", sum("totalWords", "$words"))));

printResult("", iterable);

//TODO: 將在這里填充更多聚合示例

}

//打印聚合結果

public void printResult(String doing, AggregateIterable iterable) {

System.out.println(doing);

iterable.forEach(new Block() {

public void apply(final Document document) {

System.out.println(document);

}

});

System.out.println("------------------------------------------------------");

System.out.println();

}

}

如上面代碼所示,將把所有的聚合操作集中在show()方法中演示,並且在執行后打印結果集以觀察執行結果。下面用常用的聚合代碼填充show()方法

注意需要靜態導入:

import static com.mongodb.client.model.Accumulators.*;

import static com.mongodb.client.model.Aggregates.*;

// $match 確定復合條件的文檔, 可組合多個條件

iterable = mc.aggregate(Arrays.asList(match(and(eq("owner", "tom"), gt("words", 300)))));

printResult("$match only", iterable);

// $sum求和 $avg平均值 $max最大值 $min最小值

iterable = mc.aggregate(Arrays.asList(

match(in("owner", "tom", "john", "mike")),

group("$owner", sum("totalWords", "$words"),

avg("averageWords", "$words"),

max("maxWords", "$words"), min("minWords", "$words"))));

printResult("$sum $avg $max $min", iterable);

// $out 把聚合結果輸出到集合

mc.aggregate(Arrays.asList(

match(in("owner", "tom", "john", "mike")),

group("$owner", sum("totalWords", "$words"),

avg("averageWords", "$words"),

max("maxWords", "$words"), min("minWords", "$words")),

out("wordsCount")));

iterable = database.getCollection("wordsCount").aggregate(

Arrays.asList(sample(3)));

printResult("$out", iterable);

// 隨機取3個文檔, 僅返回title和owner字段

iterable = mc.aggregate(Arrays.asList(sample(3),

project(fields(include("title", "owner"), excludeId()))));

printResult("sample(3)", iterable);

// 從第2個文檔開始取2個文檔, 僅返回title和owner字段

iterable = mc.aggregate(Arrays.asList(skip(1), limit(2),

project(fields(include("title", "owner"), excludeId()))));

printResult("skip(1), limit(2)", iterable);

// $lookup 和另一個集合關聯

database.getCollection("scores").drop();

database.getCollection("scores").insertMany(

Arrays.asList(

new Document("writer", "tom").append("score", 100),

new Document("writer", "joe").append("score", 95),

new Document("writer", "john").append("score", 80)));

iterable = mc.aggregate(Arrays.asList(lookup("scores", "owner",

"writer", "joinedOutput")));

printResult("lookup", iterable);

// 拆分comments為單個文檔

iterable = mc.aggregate(Arrays.asList(match(size("comments", 2)),

project(fields(include("comments"), excludeId())),

unwind("$comments")));

printResult("unwind comments", iterable);

System.out.println("distinct");

DistinctIterable di = mc.distinct("owner", String.class);

di.forEach(new Block() {

public void apply(final String str) {

System.out.println(str);

}

});

System.out.println("------------------------------------------------------");

System.out.println();

System.out.println("count");

long count = mc.count(Filters.eq("owner", "tom"));

System.out.println("count=" + count);

System.out.println("------------------------------------------------------");

System.out.println();

System.out.println("mapreduce");

String map = "function() { var category; "

+ "if ( this.words >= 280 ) category = 'Long blogs'; "

+ "else category = 'Short blogs'; "

+ "emit(category, {title: this.title});}";

String reduce = "function(key, values) { var cnt = 0; "

+ "values.forEach(function(doc) { cnt += 1; }); "

+ "return {count: cnt};} ";

MapReduceIterable mi = mc.mapReduce(map, reduce);

mi.forEach(new Block() {

public void apply(final Document str) {

System.out.println(str);

}

});

System.out.println("------------------------------------------------------");

System.out.println();

(完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoDB 聚合框架提供了一种数据处理方式,它能够对集合中的文档进行分组、筛选、投影、排序、限制以及多表关联等操作,从而生成新的文档集合。下面是几种 MongoDB 聚合查询方式的详解: 1. $match 查询 $match 查询通过筛选文档中的字段来过滤数据,类似于 SQL 中的 WHERE 子句。可以使用各种比较运算符、逻辑运算符和正则表达式等条件来实现高级查询。例如,以下代码将返回所有 age 大于等于 18 的文档: ``` db.collection.aggregate([ { $match : { age : { $gte : 18 } } } ]) ``` 2. $group 查询 $group 查询通过将文档分组来聚合数据。可以使用 $sum、$avg、$min、$max 等聚合运算符来计算每个分组的结果。例如,以下代码将返回每个国家的总人口数: ``` db.collection.aggregate([ { $group : { _id : "$country", population: { $sum : "$population" } } } ]) ``` 3. $project 查询 $project 查询用于投影文档中的字段,类似于 SQL 中的 SELECT 子句。可以使用 $addFields、$subtract、$multiply、$divide 等运算符来进行计算或添加新的字段。例如,以下代码将返回包含 name 和 age 字段的文档: ``` db.collection.aggregate([ { $project : { name : 1, age : 1 } } ]) ``` 4. $sort 查询 $sort 查询用于对文档进行排序,类似于 SQL 中的 ORDER BY 子句。可以使用 1 或 -1 来指定升序或降序排列。例如,以下代码将按 age 字段降序排列文档: ``` db.collection.aggregate([ { $sort : { age : -1 } } ]) ``` 以上是 MongoDB 聚合查询的几种方式,它们可以组合使用来实现更复杂的查询。在 Java 中,可以使用 MongoDBJava 驱动程序来进行聚合查询。具体的实现方式可以参考 MongoDB 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值