UnionWith Operation¶
UnionWithOperation đại diện cho stage $unionWith của MongoDB (từ 4.4). Stage này hợp nhất dữ liệu từ collection khác (và tuỳ chọn áp dụng pipeline) vào luồng hiện tại.
import { UnionWithOperation } from 'red-aggregation/operations/unionWithOperation';
import { AggregationPipeline } from 'red-aggregation/aggregationPipeline';
import { AggregateOperation } from 'red-aggregation/aggregateOperation';
Phương thức¶
unionWith(collection)¶
Tạo $unionWith tối giản, chỉ chỉ định collection đích.
UnionWithOperation
.unionWith('archivedOrders')
.toDocument(context);
// => { $unionWith: { coll: 'archivedOrders' } }
unionWith + pipeline¶
Có thể bổ sung pipeline để biến đổi dữ liệu của collection ghép vào.
// Truyền sẵn một AggregationPipeline
const pipeline = AggregationPipeline.of(new MatchOp({ $match: { status: 'ACTIVE' } }));
new UnionWithOperation('archivedOrders', pipeline).toDocument(context);
// => {
// $unionWith: {
// coll: 'archivedOrders',
// pipeline: [[ { $match: { status: 'ACTIVE' } } ]]
// }
// }
// Hoặc dùng overload variadic để truyền trực tiếp các stage
UnionWithOperation
.unionWith('archivedOrders')
.pipeline(
new MatchOp({ $match: { type: 'A' } }),
new ProjectOp({ $project: { type: 1 } })
)
.toDocument(context);
// => {
// $unionWith: {
// coll: 'archivedOrders',
// pipeline: [ [{ $match: { type: 'A' }}], [{ $project: { type: 1 }}] ]
// }
// }
Lưu ý sử dụng¶
collectionlà bắt buộc và không đượcnull.- Nếu không truyền pipeline hoặc pipeline rỗng, chỉ có
{ coll: <collection> }trong$unionWith. - Khi truyền pipeline, các stage sẽ được render bằng
toDocument(context)theoAggregationPipeline; mỗi stage được bọc trong một mảng con. - Nếu context hiện tại là
ExposedFieldsAggregationOperationContext,UnionWithOperationsẽ sử dụng root-context nội bộ khi render pipeline để tránh tác động của ánh xạ field tạm thời.