[求助] Java 操作 mongodb - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wushigejiajia01
V2EX    Java

[求助] Java 操作 mongodb

  •  
  •   wushigejiajia01 2020-11-24 18:59:10 +08:00 via Android 2976 次点击
    这是一个创建于 1835 天前的主题,其中的信息可能已经有所发展或是发生改变。

    db.chat_message.aggregate([

    { $match: { "sessionId":{$in: ["1249264194773536859"]}, "channel": 3, "senderType": 2 } }, { $grup: { _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 代码实现,网上找了一些教程看了,但是最后还是不行,没法了厚脸皮来求助

    第 1 条附言    2020-11-25 09:23:28 +08:00
    其实最初想用下面这个方式去实现的:

    Aggregation agg = newAggregation(
    match(new Criteria().andOperator(Criteria.where("sessionId").in(sessionIdList),
    Criteria.where("channel").is(3), Criteria.where("senderType").is(2))),
    group("sessionId" ,"targetId").count().as("count"),
    match(new Criteria("count").gt(2)),
    group("null").count().as("threeCount")
    );

    但是不会用, 而且这里应该还是错的,
    最终没法, 用了跟 1 楼类似的方式去实现了

    求问哪位大佬, 能帮忙看看用这样的方式怎么写吗?
    11 条回复    2020-11-25 22:58:58 +08:00
    narmgalaxy
        1
    narmgalaxy  
       2020-11-24 20:04:09 +08:00   2
    // 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
        2
    0x9527  
       2020-11-24 21:01:19 +08:0 via Android
    tcount: { $gt: 2 } ?
    wushigejiajia01
        3
    wushigejiajia01  
    OP
       2020-11-24 23:08:29 +08:00 via Android
    @0x9527 这个是手抖,弄错了,多了个 t
    wushigejiajia01
        4
    wushigejiajia01  
    OP
       2020-11-24 23:11:44 +08:00 via Android
    @narmgalaxy 感谢

    我晚上后来自己也拼出来了
    wushigejiajia01
        5
    wushigejiajia01  
    OP
       2020-11-25 09:26:23 +08:00
    @narmgalaxy

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

    但是我记得这个是只能用 SQL 生成 Java 代码吧?
    咋用 mongo shell 生成 java 代码啊?
    narmgalaxy
        6
    narmgalaxy  
       2020-11-25 09:26:47 +08:00
    我这个是自动生成的。用的 studio 3t.
    narmgalaxy
        7
    narmgalaxy  
       2020-11-25 09:28:14 +08:00
    @wushigejiajia01 不是好像,这个就是。
    wushigejiajia01
        8
    wushigejiajia01  
    OP
       2020-11-25 10:37:04 +08:00
    @narmgalaxy
    好吧 我说看着格式怪眼熟

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

    你这个是咋弄的?
    我的版本不对吗
    我的是 2019.2.1(试用版)
    narmgalaxy
        9
    narmgalaxy  
       2020-11-25 10:44:38 +08:00   1
    我也是一样的版本。
    在 aggreagate 里操作,
    wushigejiajia01
        10
    wushigejiajia01  
    OP
       2020-11-25 10:58:18 +08:00
    @narmgalaxy
    哈哈
    我自己研究了下, 搞出来了
    我就想着赶紧过来回复你一下, 没想到你这么快就回复了

    哈哈 感谢
    qinxi
        11
    qinxi  
       2020-11-25 22:58:58 +08:00
    //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();
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2967 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 00:32 PVG 08:32 LAX 16:32 JFK 19:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86