MongoDB Compass 导出管道到其他风格的语言
MongoDB Compass Export Pipeline to Language with other style
在 MongoDB Compass 中,当我选择将聚合管道导出到 Java 时,我得到如下信息:
Arrays.asList(new Document("$group",
new Document("_id", "$loginTime.seconds")
.append("loginTime",
new Document("$min", "$loginTime.seconds"))))
虽然这似乎是正确的,但我想知道如何生成等效表达式:
List.of(Aggregates.group("$loginTime.seconds", Accumulators.min("loginTime", "$loginTime.seconds)));
显然后者更直接、更明确,足以让人一见倾心。但是 MongoDB Compass 没有为此提供任何选项。为什么。
我根据@prasad_ 的评论做了一些研究
mongoDB 问题跟踪器上似乎有相关问题
见 https://jira.mongodb.org/browse/COMPASS-4695
我还认为这将是一个非常好的功能,因为较长的聚合只是不可读的,如果我们需要调整它们,我们必须经常 re-engineer 它们。
您是否考虑过将聚合阶段保存为数据库中的视图?然后管道存储在 mongodb 中,您可以像查询任何其他集合一样查询它。
除此之外,我考虑过将管道导出为 js / json / 或原始管道(我更喜欢 js/json 代替 auto-formatting),然后从资源文件夹中解析它。
当然,这意味着您必须想出一种机制来替换查询的动态部分,这可能会导致安全问题,但也许您可以分解非动态部分。
Aggregation aggregation =
newAggregation(
CustomDocumentAggregationOperation.of(
matchByIdAndOtherValues(objectId, otherValues),
lookupSomeDataOfOtherCollection(),
unwindSomething(),
projectToSomething()));
我在我的存储库中采用了这种模式,以便我可以第一眼看到聚合在做什么。隐藏在这些静态函数后面的是一堆bson.Documents,我尽量避免去那里...
这就是我 assemble 多个文档到一个 AggregationOperation
的方式
public class CustomDocumentAggregationOperation implements AggregationOperation {
private final Document document;
public CustomDocumentAggregationOperation(Document document) {
this.document = document;
}
@Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
return aggregationOperationContext.getMappedObject(document);
}
public static AggregationOperation of(Document document) {
return new CustomDocumentAggregationOperation(document);
}
public static List<AggregationOperation> of(@NonNull Document... documents) {
return Arrays.stream(documents)
.map(CustomDocumentAggregationOperation::of)
.collect(Collectors.toList());
}
在 MongoDB Compass 中,当我选择将聚合管道导出到 Java 时,我得到如下信息:
Arrays.asList(new Document("$group",
new Document("_id", "$loginTime.seconds")
.append("loginTime",
new Document("$min", "$loginTime.seconds"))))
虽然这似乎是正确的,但我想知道如何生成等效表达式:
List.of(Aggregates.group("$loginTime.seconds", Accumulators.min("loginTime", "$loginTime.seconds)));
显然后者更直接、更明确,足以让人一见倾心。但是 MongoDB Compass 没有为此提供任何选项。为什么。
我根据@prasad_ 的评论做了一些研究 mongoDB 问题跟踪器上似乎有相关问题 见 https://jira.mongodb.org/browse/COMPASS-4695
我还认为这将是一个非常好的功能,因为较长的聚合只是不可读的,如果我们需要调整它们,我们必须经常 re-engineer 它们。
您是否考虑过将聚合阶段保存为数据库中的视图?然后管道存储在 mongodb 中,您可以像查询任何其他集合一样查询它。
除此之外,我考虑过将管道导出为 js / json / 或原始管道(我更喜欢 js/json 代替 auto-formatting),然后从资源文件夹中解析它。
当然,这意味着您必须想出一种机制来替换查询的动态部分,这可能会导致安全问题,但也许您可以分解非动态部分。
Aggregation aggregation =
newAggregation(
CustomDocumentAggregationOperation.of(
matchByIdAndOtherValues(objectId, otherValues),
lookupSomeDataOfOtherCollection(),
unwindSomething(),
projectToSomething()));
我在我的存储库中采用了这种模式,以便我可以第一眼看到聚合在做什么。隐藏在这些静态函数后面的是一堆bson.Documents,我尽量避免去那里...
这就是我 assemble 多个文档到一个 AggregationOperation
的方式 public class CustomDocumentAggregationOperation implements AggregationOperation {
private final Document document;
public CustomDocumentAggregationOperation(Document document) {
this.document = document;
}
@Override
public Document toDocument(AggregationOperationContext aggregationOperationContext) {
return aggregationOperationContext.getMappedObject(document);
}
public static AggregationOperation of(Document document) {
return new CustomDocumentAggregationOperation(document);
}
public static List<AggregationOperation> of(@NonNull Document... documents) {
return Arrays.stream(documents)
.map(CustomDocumentAggregationOperation::of)
.collect(Collectors.toList());
}