3838#include " mongo/db/exec/filter.h"
3939#include " mongo/db/exec/scoped_timer.h"
4040#include " mongo/db/exec/working_set_common.h"
41+ #include " mongo/db/s/sharding_state.h"
4142#include " mongo/s/shard_key_pattern.h"
4243#include " mongo/util/log.h"
4344
@@ -53,8 +54,10 @@ const char* ShardFilterStage::kStageType = "SHARDING_FILTER";
5354ShardFilterStage::ShardFilterStage (OperationContext* opCtx,
5455 ScopedCollectionMetadata metadata,
5556 WorkingSet* ws,
56- std::unique_ptr<PlanStage> child)
57- : PlanStage(kStageType , opCtx), _ws(ws), _shardFilterer(std::move(metadata)) {
57+ std::unique_ptr<PlanStage> child,
58+ bool wantShardName)
59+ : PlanStage(kStageType , opCtx), _ws(ws), _shardFilterer(std::move(metadata)),
60+ _wantShardName (wantShardName) {
5861 _children.emplace_back (std::move (child));
5962}
6063
@@ -104,6 +107,13 @@ PlanStage::StageState ShardFilterStage::doWork(WorkingSetID* out) {
104107 ++_specificStats.chunkSkips ;
105108 return PlanStage::NEED_TIME;
106109 }
110+
111+ if (wantShardName ()) {
112+ auto sharding = ShardingState::get (this ->getOpCtx ());
113+
114+ // Populate the working set member with the shard name and return it.
115+ member->metadata ().setShardName (sharding->shardId ());
116+ }
107117 }
108118
109119 // If we're here either we have shard state and our doc passed, or we have no shard
0 commit comments