mongodb - 持久化聚合结果
问题描述
我们尝试在返回之前将集合操作的结果保存到单独的集合中。
Aggregates.project(
Projections.fields(
Projections.computed("diff",
Document.parse("{ $setDifference: [{\"local\", \"$global\"] }"))
,Projections.excludeId()
)
)
,Aggregates.unwind("$diff",new UnwindOptions().preserveNullAndEmptyArrays(true))
,Aggregates.out("globaldata")
但是,这似乎取代了全局数据集合,而不是附加到它。
Mongo 3.6 中是否有出路($merge 仅适用于 4.2.x)
解决方案
还没有@IUnknown,但在探索后发现其中一位 MongoDB 开发人员在这里写道,他们已经为$out
IE;
$out: {
mode: "replaceCollection" | "replaceDocuments" | "insertDocuments",
to: "targetCollection",
uniqueKey: {<set of fields that uniquely identify the document>},
}
但是在阅读了 MongoDB 的官方文档后,我找不到 $out 的任何新更新。但是,您将看到新的$merge
管道,它提供了与在此处附加文档相同的功能。
在 MongoDB 4.2 中
{ $merge: {
into: <collection> -or- { db: <db>, coll: <collection> },
on: <identifier field> -or- [ <identifier field1>, ...], // Optional
let: <variables>, // Optional
whenMatched: <replace|keepExisting|merge|fail|pipeline>, // Optional
whenNotMatched: <insert|discard|fail> // Optional
} }
因此,您最好更新您的 MongoDB 版本。
推荐阅读
- c# - 如何在 WPF 中的组合框选择上禁用扩展器?
- drupal - Drupal 6站点重新索引没有发生
- git - 将分支从 svn 移动到 git
- python - 如何使用 Python 列出 .txt 到 .csv 格式的字符串?
- jquery - 数据表过滤器服务器端 Laravel
- mysql - SSIS - 设置 MySQL 连接管理器(具有 SSH 隧道)
- java - java 8从u60自动更新到u181:只有JRE
- javascript - Angular 4 在单击仅第一次指向其他组件的锚标记时加载默认组件
- pointers - 为什么通过原始指针修改可变引用的值不会违反 Rust 的别名规则?
- javascript - onChange 不会在 React 组件中触发