设为首页 收藏本站
查看: 780|回复: 0

[经验分享] MongoDB-JAVA

[复制链接]

尚未签到

发表于 2017-12-16 12:31:36 | 显示全部楼层 |阅读模式
  转载,原文连接:http://blog.csdn.net/autfish/article/details/51379379
  MongoDB的3.x版本Java驱动相对2.x做了全新的设计,类库和使用方法上有很大区别。例如用Document替换BasicDBObject、通过Builders类构建Bson替代直接输入$命令等,本文整理了基于3.2版本的常用增删改查操作的使用方法。为了避免冗长的篇幅,分为增删改、查询、聚合、地理索引等几部分。
  聚合用于统计文档个数、求和、最大最小值、求平均值等,功能和函数名称和SQL中的count、distinct、group等关键字非常类似,此外,还可以通过JavaScript编写MapReduce实现复杂的计算(性能损耗也会非常严重)。
  首先来看3.x驱动中的聚合方法的声明:
  AggregateIterable<TDocument> 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并对字数求和。
  下面来看更多命令用法,用于演示的类的基本代码如下
[java] view plain copy  print?

  • 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<Document> 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<Document> iterable = mc.aggregate(Arrays.asList(match(eq("owner", "tom")),
  •                 group("$author", sum("totalWords", "$words"))));
  •         printResult("", iterable);

  •         //TODO: 将在这里填充更多聚合示例
  •     }

  •     //打印聚合结果
  •     public void printResult(String doing, AggregateIterable<Document> iterable) {
  •         System.out.println(doing);
  •         iterable.forEach(new Block<Document>() {
  •             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.*;
[java] view plain copy  print?

  • // $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<String> di = mc.distinct("owner", String.class);
  • di.forEach(new Block<String>() {
  •     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<Document> mi = mc.mapReduce(map, reduce);
  • mi.forEach(new Block<Document>() {
  •     public void apply(final Document str) {
  •         System.out.println(str);
  •     }
  • });
  • System.out.println("------------------------------------------------------");
  • System.out.println();
  (完)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-424682-1-1.html 上篇帖子: MongoDB + express + node + bootstrap 搭建多人博客 下篇帖子: Python连接mongodb提取部分字段内数据并写入txt文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表