[求助] Java 操作 mongodb

2020-11-24 18:59:10 +08:00
 wushigejiajia01

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 代码实现,网上找了一些教程看了,但是最后还是不行,没法了厚脸皮来求助

2343 次点击
所在节点    Java
11 条回复
narmgalaxy
2020-11-24 20:04:09 +08:00
// 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
2020-11-24 21:01:19 +08:00
tcount: { $gt: 2 } ?
wushigejiajia01
2020-11-24 23:08:29 +08:00
@0x9527 这个是手抖,弄错了,多了个 t
wushigejiajia01
2020-11-24 23:11:44 +08:00
@narmgalaxy 感谢😘😚😗😙

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

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

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

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

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

哈哈 感谢
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();

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/728827

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX