跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • [求助] Java 操作 mongodb
未分類
25 11 月 2020

[求助] Java 操作 mongodb

[求助] Java 操作 mongodb

資深大佬 : wushigejiajia01 3

db.chat_message.aggregate([

{      $match: {          "sessionId":{$in: ["1249264194773536859"]},           "channel": 3,          "senderType": 2      }  },  {      $group: {          _id: {              sessionId: "$sessionId",              targetId: "$targetId"          },          count: { $sum: 1 }      }  },  {      $match: {          tcount: { $gt: 2 }      }  },  {      $group: {          _id: {              targetId: "$_id.targetId"          },          count: { $sum: 1}      }  },  {      $group: {          _id: null,          count: {$sum: 1}      }  } 

])

第一次搞 mongdb,还是这种多重分组的,要用 java 代码实现,网上找了一些教程看了,但是最后还是不行,没法了厚脸皮来求助

大佬有話說 (11)

  • 資深大佬 : narmgalaxy

    // Requires official Java MongoDB Driver 3.6+
    import com.mongodb.Block;
    import com.mongodb.MongoClient;
    import com.mongodb.MongoException;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoDatabase;
    import java.util.Arrays;
    import java.util.List;
    import org.bson.BsonNull;
    import org.bson.Document;
    import org.bson.conversions.Bson;

    public class Program {

    public static void main(String[] args) {

    try (MongoClient client = new MongoClient(“localhost”, 27017)) {

    MongoDatabase database = client.getDatabase(“bijiduo”);
    MongoCollection<Document> collection = database.getCollection(“docInfo”);

    // Created with Studio 3T, the IDE for MongoDB – https://studio3t.com/

    Block<Document> processBlock = new Block<Document>() {
    @Override
    public void apply(final Document document) {
    System.out.println(document);
    }
    };

    List<? extends Bson> pipeline = Arrays.asList(
    new Document()
    .append(“$match”, new Document()
    .append(“sessionId”, new Document()
    .append(“$in”, Arrays.asList(
    “1249264194773536859”
    )
    )
    )
    .append(“channel”, 3.0)
    .append(“senderType”, 2.0)
    ),
    new Document()
    .append(“$group”, new Document()
    .append(“_id”, new Document()
    .append(“sessionId”, “$sessionId”)
    .append(“targetId”, “$targetId”)
    )
    .append(“count”, new Document()
    .append(“$sum”, 1.0)
    )
    ),
    new Document()
    .append(“$match”, new Document()
    .append(“tcount”, new Document()
    .append(“$gt”, 2.0)
    )
    ),
    new Document()
    .append(“$group”, new Document()
    .append(“_id”, new Document()
    .append(“targetId”, “$_id.targetId”)
    )
    .append(“count”, new Document()
    .append(“$sum”, 1.0)
    )
    ),
    new Document()
    .append(“$group”, new Document()
    .append(“_id”, new BsonNull())
    .append(“count”, new Document()
    .append(“$sum”, 1.0)
    )
    )
    );

    collection.aggregate(pipeline)
    .allowDiskUse(false)
    .forEach(processBlock);

    } catch (MongoException e) {
    // handle MongoDB exception
    }
    }

    }

  • 資深大佬 : 0x9527

    tcount: { $gt: 2 } ?

  • 主 資深大佬 : wushigejiajia01

    @0x9527 这个是手抖,弄错了,多了个 t

  • 主 資深大佬 : wushigejiajia01

    @narmgalaxy 感谢

  • 主 資深大佬 : wushigejiajia01

    @narmgalaxy

    大佬 你这个是用 studio 3T 生成的吗? 格式好像

    但是我记得这个是只能用 SQL 生成 Java 代码吧?
    咋用 mongo shell 生成 java 代码啊?

  • 資深大佬 : narmgalaxy

    我这个是自动生成的。用的 studio 3t.

  • 資深大佬 : narmgalaxy

    @wushigejiajia01 不是好像,这个就是。

  • 主 資深大佬 : wushigejiajia01

    @narmgalaxy
    好吧 我说看着格式怪眼熟

    可是 我用的版本好像只有从 SQL 生成 Java 代码, 没有从 shell 生成代码的

    你这个是咋弄的?
    我的版本不对吗
    我的是 2019.2.1(试用版)

  • 資深大佬 : narmgalaxy

    我也是一样的版本。
    在 aggreagate 里操作,

  • 主 資深大佬 : wushigejiajia01

    @narmgalaxy
    哈哈
    我自己研究了下, 搞出来了
    我就想着赶紧过来回复你一下, 没想到你这么快就回复了

    哈哈 感谢

  • 資深大佬 : qinxi

    //match1
    final MatchOperation match = Aggregation.match(criteria);
    //group1
    final GroupOperation group1 = Aggregation.group(“sessionId”,”targetId”).count().as(“count”);
    //match2
    final MatchOperation match2 = Aggregation.match(new Criteria(“count”).gt(2));

    //group2
    Aggregation.group().count().as(“count”);

    final Aggregation aggregation = Aggregation.newAggregation(match1, group1,match2, group2);

    final Map uniqueMappedResult = mongoTemplate.aggregate(aggregation, UserTask.class, Map.class).getUniqueMappedResult();

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具