将文档从一个集合移动到另一个集合会覆盖文档
Move document from one collection to another is overwriting the document
第一个操作是匹配id[1602271],创建新的集合并保存一个文档(下面提到的文档)。
{
"_id": "1602271",
"date": "2019-02-11T06:25:13.425Z",
"currentStatus": "scheduled",
"statusHistory": [
{
"status": "onboarded",
"date": "2018-11-02T10:07:11.167Z"
},
{
"status": "preference_ready",
"date": "2018-11-02T10:08:56.359Z"
},
{
"status": "scheduled",
"date": "2018-11-02T10:26:38.721Z"
}
]
}
对于第二个操作 id[1602131],它不会创建新文档,而是用旧文档覆盖(JSON 以上)。
{
"_id": "1602131",
"date": "2019-01-22T07:08:58.253Z",
"currentStatus": "scheduled",
"statusHistory": [
{
"status": "onboarded",
"date": "2018-11-02T06:07:28.765Z"
},
{
"status": "preference_ready",
"date": "2018-11-02T06:11:30.777Z"
},
{
"status": "scheduled",
"date": "2018-11-29T05:48:57.871Z"
}
]
}
请参考以下代码:
public static final String STATUS_COLLECTION_NAME = "TeacherStatus";
public static final String ARCHIVE_STATUS_COLLECTION_NAME =
"ArchiveTeacherStatus";
Aggregation aggregation = Aggregation.newAggregation(match(where("_id").is(teacherId)),
out(ARCHIVE_STATUS_COLLECTION_NAME));
mongoOperations.aggregate(aggregation, STATUS_COLLECTION_NAME, TeacherStatus.class);
按预期工作。 https://docs.mongodb.com/manual/reference/operator/aggregation/out/
If the collection specified by the $out
operation already exists, then upon completion of the aggregation, the $out
stage atomically replaces the existing collection with the new results collection.
在 mongodb 4.2 中,$out
阶段将接受一个附加参数 mode
,它可以取值 "replaceCollection"(现在发生了什么),"replaceDocuments", "insertDocuments" (你想要什么).
有了 re-read 你的代码,你为什么要使用 聚合管道 和 $out
来复制 一个 文件?那是用大炮打麻雀
您可以通过该应用程序更可靠地做到这一点。阅读文档,然后保存到其他collection.
第一个操作是匹配id[1602271],创建新的集合并保存一个文档(下面提到的文档)。
{
"_id": "1602271",
"date": "2019-02-11T06:25:13.425Z",
"currentStatus": "scheduled",
"statusHistory": [
{
"status": "onboarded",
"date": "2018-11-02T10:07:11.167Z"
},
{
"status": "preference_ready",
"date": "2018-11-02T10:08:56.359Z"
},
{
"status": "scheduled",
"date": "2018-11-02T10:26:38.721Z"
}
]
}
对于第二个操作 id[1602131],它不会创建新文档,而是用旧文档覆盖(JSON 以上)。
{
"_id": "1602131",
"date": "2019-01-22T07:08:58.253Z",
"currentStatus": "scheduled",
"statusHistory": [
{
"status": "onboarded",
"date": "2018-11-02T06:07:28.765Z"
},
{
"status": "preference_ready",
"date": "2018-11-02T06:11:30.777Z"
},
{
"status": "scheduled",
"date": "2018-11-29T05:48:57.871Z"
}
]
}
请参考以下代码:
public static final String STATUS_COLLECTION_NAME = "TeacherStatus";
public static final String ARCHIVE_STATUS_COLLECTION_NAME =
"ArchiveTeacherStatus";
Aggregation aggregation = Aggregation.newAggregation(match(where("_id").is(teacherId)),
out(ARCHIVE_STATUS_COLLECTION_NAME));
mongoOperations.aggregate(aggregation, STATUS_COLLECTION_NAME, TeacherStatus.class);
按预期工作。 https://docs.mongodb.com/manual/reference/operator/aggregation/out/
If the collection specified by the
$out
operation already exists, then upon completion of the aggregation, the$out
stage atomically replaces the existing collection with the new results collection.
在 mongodb 4.2 中,$out
阶段将接受一个附加参数 mode
,它可以取值 "replaceCollection"(现在发生了什么),"replaceDocuments", "insertDocuments" (你想要什么).
有了 re-read 你的代码,你为什么要使用 聚合管道 和 $out
来复制 一个 文件?那是用大炮打麻雀
您可以通过该应用程序更可靠地做到这一点。阅读文档,然后保存到其他collection.