Java Mongo 聚合查询的语法
Java Syntax for a Mongo Aggregation Query
我对 Mongo 查询的 Java 语法感到困惑。
我已经为控制台编写了 Mongo 查询,但我想要该查询的相应 Java 语法。
这是 shell 命令:
db.errorsegmentaudit.aggregate([
{$sort:{timestamp:1}},
{$limit:1},
{$unwind:"$auditErrorTypeCounts"},
{$unwind:"$auditErrorTypeCounts.auditErrorCounts"},
{$group: {
_id: {
"agent": "$auditErrorTypeCounts.auditErrorCounts.agentName",
"type":"$auditErrorTypeCounts.typeOfError"
},
count: {
$sum: "$auditErrorTypeCounts.auditErrorCounts.countOfErrors"
}
}
}
])
我已经仔细阅读了所有文档,但没有找到合适的文档。我尝试在 Java 中使用聚合 class 但它没有用。
MongoDB Java 驱动程序(使用版本 3.x)相当于你问题中的 MongoDB shell 命令是:
MongoClient mongoClient = ...;
MongoCollection<Document> collection = mongoClient.getDatabase("...").getCollection("..");
AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
// $sort:{timestamp:1}
Aggregates.sort(Sorts.ascending("timestamp")),
// $limit:1
Aggregates.limit(1),
// $unwind:"$auditErrorTypeCounts"
Aggregates.unwind("$auditErrorTypeCounts"),
// $unwind:"$auditErrorTypeCounts.auditErrorCounts"
Aggregates.unwind("$auditErrorTypeCounts.auditErrorCounts"),
// $group:{...}
Aggregates.group(
// _id:{"agent":"$auditErrorTypeCounts.auditErrorCounts.agentName","type":"$auditErrorTypeCounts.typeOfError"}
new Document("agent", "$auditErrorTypeCounts.auditErrorCounts.agentName").append("type", "$auditErrorTypeCounts.typeOfError"),
// count:{$sum:"$auditErrorTypeCounts.auditErrorCounts.countOfErrors"}}}
Accumulators.sum("count", "$auditErrorTypeCounts.auditErrorCounts.countOfErrors")
)
));
聚合管道中每个阶段上方的注释显示 shell 命令 Java 代码镜像。例如:
// $unwind:"$auditErrorTypeCounts.auditErrorCounts"
Aggregates.unwind("$auditErrorTypeCounts.auditErrorCounts")
... 显示 Java 相当于 shell 命令 $unwind:"$auditErrorTypeCounts.auditErrorCounts"
是 Aggregates.unwind("$auditErrorTypeCounts.auditErrorCounts")
。这可能使您更容易将一个映射到另一个。
MongoDB Java driver docs 中有更多详细信息。
我对 Mongo 查询的 Java 语法感到困惑。
我已经为控制台编写了 Mongo 查询,但我想要该查询的相应 Java 语法。
这是 shell 命令:
db.errorsegmentaudit.aggregate([
{$sort:{timestamp:1}},
{$limit:1},
{$unwind:"$auditErrorTypeCounts"},
{$unwind:"$auditErrorTypeCounts.auditErrorCounts"},
{$group: {
_id: {
"agent": "$auditErrorTypeCounts.auditErrorCounts.agentName",
"type":"$auditErrorTypeCounts.typeOfError"
},
count: {
$sum: "$auditErrorTypeCounts.auditErrorCounts.countOfErrors"
}
}
}
])
我已经仔细阅读了所有文档,但没有找到合适的文档。我尝试在 Java 中使用聚合 class 但它没有用。
MongoDB Java 驱动程序(使用版本 3.x)相当于你问题中的 MongoDB shell 命令是:
MongoClient mongoClient = ...;
MongoCollection<Document> collection = mongoClient.getDatabase("...").getCollection("..");
AggregateIterable<Document> documents = collection.aggregate(Arrays.asList(
// $sort:{timestamp:1}
Aggregates.sort(Sorts.ascending("timestamp")),
// $limit:1
Aggregates.limit(1),
// $unwind:"$auditErrorTypeCounts"
Aggregates.unwind("$auditErrorTypeCounts"),
// $unwind:"$auditErrorTypeCounts.auditErrorCounts"
Aggregates.unwind("$auditErrorTypeCounts.auditErrorCounts"),
// $group:{...}
Aggregates.group(
// _id:{"agent":"$auditErrorTypeCounts.auditErrorCounts.agentName","type":"$auditErrorTypeCounts.typeOfError"}
new Document("agent", "$auditErrorTypeCounts.auditErrorCounts.agentName").append("type", "$auditErrorTypeCounts.typeOfError"),
// count:{$sum:"$auditErrorTypeCounts.auditErrorCounts.countOfErrors"}}}
Accumulators.sum("count", "$auditErrorTypeCounts.auditErrorCounts.countOfErrors")
)
));
聚合管道中每个阶段上方的注释显示 shell 命令 Java 代码镜像。例如:
// $unwind:"$auditErrorTypeCounts.auditErrorCounts"
Aggregates.unwind("$auditErrorTypeCounts.auditErrorCounts")
... 显示 Java 相当于 shell 命令 $unwind:"$auditErrorTypeCounts.auditErrorCounts"
是 Aggregates.unwind("$auditErrorTypeCounts.auditErrorCounts")
。这可能使您更容易将一个映射到另一个。
MongoDB Java driver docs 中有更多详细信息。