mongodb聚合查询实现功能,统计多个组织机构下短信发送条数,组织机构字段为sendOrgCode,要根据短信发送状态 sendStatus 统计出成功条数、失败条数、总条数,查询结果返回主键用java怎么写
时间: 2023-04-10 08:02:30 浏览: 76
您可以使用以下Java代码来实现mongodb聚合查询:
```
MongoCollection<Document> collection = database.getCollection("yourCollectionName");
List<Document> pipeline = Arrays.asList(
new Document("$match", new Document("sendOrgCode", "yourOrgCode")),
new Document("$group", new Document("_id", "$sendStatus")
.append("count", new Document("$sum", 1))),
new Document("$project", new Document("_id", 0)
.append("status", "$_id")
.append("count", 1))
);
MongoCursor<Document> cursor = collection.aggregate(pipeline).iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
String status = doc.getString("status");
int count = doc.getInteger("count");
// do something with status and count
}
```
请将“yourCollectionName”替换为您的集合名称,“yourOrgCode”替换为您要查询的组织机构代码。此代码将返回一个包含成功、失败和总条数的文档列表。