
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 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 } } } |
2 0x9527 2020-11-24 21:01:19 +08:0 via Android tcount: { $gt: 2 } ? |
3 wushigejiajia01 OP @0x9527 这个是手抖,弄错了,多了个 t |
4 wushigejiajia01 OP |
5 wushigejiajia01 OP |
6 narmgalaxy 2020-11-25 09:26:47 +08:00 我这个是自动生成的。用的 studio 3t. |
7 narmgalaxy 2020-11-25 09:28:14 +08:00 @wushigejiajia01 不是好像,这个就是。 |
8 wushigejiajia01 OP @narmgalaxy 好吧 我说看着格式怪眼熟 可是 我用的版本好像只有从 SQL 生成 Java 代码, 没有从 shell 生成代码的 你这个是咋弄的? 我的版本不对吗 我的是 2019.2.1(试用版) |
9 narmgalaxy 2020-11-25 10:44:38 +08:00 我也是一样的版本。 在 aggreagate 里操作, |
10 wushigejiajia01 OP |
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(); |