MongoDB with Java

编程入门 行业动态 更新时间:2024-10-28 11:32:49
本文介绍了MongoDB with Java - GroupBy 多一栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 java 为 MongoDB 传输 PostgreSQL.

i am trying transfer the PostgreSQL for MongoDB using java.

我有 SQL

SELECT id_buyer, buyer, SUM(qtde) FROM test.log GROUP BY id_buyer, buyer

和我的新代码

MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase("teste"); MongoCollection<Document> coll = db.getCollection("log"); DBObject groupFields = new BasicDBObject(); groupFields.put("id_buyer", "$id_buyer"); groupFields.put("buyer", "$buyer"); AggregateIterable<Document> mongoCollectionList = coll.aggregate( Arrays.asList( Aggregates.group(groupFields, Accumulators.sum("qtde", "$qtde")), Aggregates.project(fields(include("comprador", "Quantidade"))) )); MongoCursor<Document> mongoCursor = mongoCollectionList.iterator(); while (mongoCursor.hasNext()) { System.out.println(mongoCursor.next().toJson()); }

结果

{ "_id" : { "id_buyer" : 2, "buyer" : "COMPS" }, "qtde" : 16703 }

如何删除_id"中的id_buyer"和buyer"?

How do i remove "id_buyer" and "buyer" that is into "_id" ?

谢谢

推荐答案

我已经测试了 答案 由 Veeram 发布,它非常适合我.

I have tested the answer posted by Veeram and it worked perfectly for me.

这是他的建议:

Aggregates.project(fields(excludeId(), computed("id_buyer", "$_id.id_buyer"), computed("buyer", "$_id.buyer"), include("comprador", "Quantidade")));

更多推荐

MongoDB with Java

本文发布于:2023-10-14 19:02:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MongoDB   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!