如何copy/clone一个mongodb集合到另一个?
How to copy/clone a mongodb collection to another one?
我正在尝试使用 spring-data 将一个集合(大的或小的)复制到同一 mongo 数据库中的另一个集合。
我发现的每个解决方案都已弃用或不起作用。
我试过这个似乎最适合我需要的:
mongoTemplate.getCollection("oneCollection").aggregate(Arrays.asList(new Document("$out","otherCollection")));
但是它没有做任何事情,没有错误,没有对数据库进行操作。
您有任何使用 spring-data 轻松复制集合的解决方案吗?
尝试:
OutOperation outOperation = new OutOperation("otherCollection");
mongoTemplate.aggregate(Aggregation.newAggregation(outOperation), "oneCollection", BasicDBObject.class);
请注意,OutOperation
目前正在 替换 目标集合。
此集合中已有的所有数据都将丢失并被此操作替换。
(Spring 的 OutOperation
有一个 OutMode
,但底层 mongo 驱动程序不支持它,如果有人对此有更多信息?)
对于那些想要将 插入 值到目标集合中,而不是替换整个集合的人:
Mongo 4.2 有 introduced 一个 $merge
操作。
在recent spring.
中被称为MergeOperation
为了使用这个新的操作,您可能需要将 mongo 驱动程序 升级到 > 4.2 的版本。它不适用于 mongodb-driver-sync:4.0.5
,它可能是默认情况下由 Spring 拉取的那个。
MergeOperation mergeOp = Aggregation.merge().intoCollection("othercollection").build();
mongoTemplate.aggregate(mergeOp, "originalcollection", BasicDBObject.class);
另见,skipOutput()
option。
我正在尝试使用 spring-data 将一个集合(大的或小的)复制到同一 mongo 数据库中的另一个集合。
我发现的每个解决方案都已弃用或不起作用。 我试过这个似乎最适合我需要的:
mongoTemplate.getCollection("oneCollection").aggregate(Arrays.asList(new Document("$out","otherCollection")));
但是它没有做任何事情,没有错误,没有对数据库进行操作。
您有任何使用 spring-data 轻松复制集合的解决方案吗?
尝试:
OutOperation outOperation = new OutOperation("otherCollection");
mongoTemplate.aggregate(Aggregation.newAggregation(outOperation), "oneCollection", BasicDBObject.class);
请注意,OutOperation
目前正在 替换 目标集合。
此集合中已有的所有数据都将丢失并被此操作替换。
(Spring 的 OutOperation
有一个 OutMode
,但底层 mongo 驱动程序不支持它,如果有人对此有更多信息?)
对于那些想要将 插入 值到目标集合中,而不是替换整个集合的人:
Mongo 4.2 有 introduced 一个 $merge
操作。
在recent spring.
MergeOperation
为了使用这个新的操作,您可能需要将 mongo 驱动程序 升级到 > 4.2 的版本。它不适用于 mongodb-driver-sync:4.0.5
,它可能是默认情况下由 Spring 拉取的那个。
MergeOperation mergeOp = Aggregation.merge().intoCollection("othercollection").build();
mongoTemplate.aggregate(mergeOp, "originalcollection", BasicDBObject.class);
另见,skipOutput()
option。