diff --git a/src/mongo/db/exec/document_value/value_comparator.h b/src/mongo/db/exec/document_value/value_comparator.h
index b85948c67a5..159f22befea 100644
--- a/src/mongo/db/exec/document_value/value_comparator.h
+++ b/src/mongo/db/exec/document_value/value_comparator.h
@@ -203,6 +203,11 @@ public:
         return stdx::unordered_map<Value, T, Hasher, EqualTo>(0, Hasher(this), EqualTo(this));
     }
 
+    template <typename T>
+    stdx::flat_unordered_map<Value, T, Hasher, EqualTo> makeFlatUnorderedValueMap() const {
+        return stdx::flat_unordered_map<Value, T, Hasher, EqualTo>(0, Hasher(this), EqualTo(this));
+    }
+
 private:
     const StringDataComparator* _stringComparator = nullptr;
 };
@@ -227,4 +232,8 @@ template <typename T>
 using ValueUnorderedMap =
     stdx::unordered_map<Value, T, ValueComparator::Hasher, ValueComparator::EqualTo>;
 
+template <typename T>
+using ValueFlatUnorderedMap =
+    stdx::flat_unordered_map<Value, T, ValueComparator::Hasher, ValueComparator::EqualTo>;
+
 }  // namespace mongo
diff --git a/src/mongo/db/exec/sbe/stages/block_hashagg.cpp b/src/mongo/db/exec/sbe/stages/block_hashagg.cpp
index 7e29b1e5042..c050b25d55b 100644
--- a/src/mongo/db/exec/sbe/stages/block_hashagg.cpp
+++ b/src/mongo/db/exec/sbe/stages/block_hashagg.cpp
@@ -147,8 +147,10 @@ struct SpanEq {
     }
 };
 
-using KeyTableType = stdx::
-    unordered_map<std::span<size_t>, std::pair<size_t, value::MaterializedRow>, SpanHasher, SpanEq>;
+using KeyTableType = stdx::flat_unordered_map<std::span<size_t>,
+                                              std::pair<size_t, value::MaterializedRow>,
+                                              SpanHasher,
+                                              SpanEq>;
 }  // namespace
 
 BlockHashAggStage::BlockHashAggStage(std::unique_ptr<PlanStage> input,
diff --git a/src/mongo/db/exec/sbe/stages/hashagg_base.h b/src/mongo/db/exec/sbe/stages/hashagg_base.h
index fdc51e6cabf..923062b6e15 100644
--- a/src/mongo/db/exec/sbe/stages/hashagg_base.h
+++ b/src/mongo/db/exec/sbe/stages/hashagg_base.h
@@ -65,10 +65,10 @@ protected:
     void doAttachToOperationContext(OperationContext* opCtx) override;
 
     using SpilledRow = std::pair<value::MaterializedRow, value::MaterializedRow>;
-    using TableType = stdx::unordered_map<value::MaterializedRow,
-                                          value::MaterializedRow,
-                                          value::MaterializedRowHasher,
-                                          value::MaterializedRowEq>;
+    using TableType = stdx::flat_unordered_map<value::MaterializedRow,
+                                               value::MaterializedRow,
+                                               value::MaterializedRowHasher,
+                                               value::MaterializedRowEq>;
 
     using HashKeyAccessor = value::MaterializedRowKeyAccessor<TableType::iterator>;
     using HashAggAccessor = value::MaterializedRowValueAccessor<TableType::iterator>;
diff --git a/src/mongo/db/pipeline/group_processor.cpp b/src/mongo/db/pipeline/group_processor.cpp
index 93fb78475e7..492ac7866f9 100644
--- a/src/mongo/db/pipeline/group_processor.cpp
+++ b/src/mongo/db/pipeline/group_processor.cpp
@@ -183,7 +183,7 @@ void GroupProcessor::readyGroups() {
             spill();
         }
 
-        _groups = _expCtx->getValueComparator().makeUnorderedValueMap<Accumulators>();
+        _groups = _expCtx->getValueComparator().makeFlatUnorderedValueMap<Accumulators>();
 
         _sorterIterator.reset(Sorter<Value, Value>::Iterator::merge(
             _sortedFiles, SortOptions(), SorterComparator(_expCtx->getValueComparator())));
diff --git a/src/mongo/db/pipeline/group_processor_base.cpp b/src/mongo/db/pipeline/group_processor_base.cpp
index 1f09cd0fcf8..f28125ecd84 100644
--- a/src/mongo/db/pipeline/group_processor_base.cpp
+++ b/src/mongo/db/pipeline/group_processor_base.cpp
@@ -42,7 +42,7 @@ GroupProcessorBase::GroupProcessorBase(const boost::intrusive_ptr<ExpressionCont
                                        int64_t maxMemoryUsageBytes)
     : _expCtx(expCtx),
       _memoryTracker{expCtx->allowDiskUse && !expCtx->inMongos, maxMemoryUsageBytes},
-      _groups(expCtx->getValueComparator().makeUnorderedValueMap<Accumulators>()) {}
+      _groups(expCtx->getValueComparator().makeFlatUnorderedValueMap<Accumulators>()) {}
 
 void GroupProcessorBase::addAccumulationStatement(AccumulationStatement accumulationStatement) {
     tassert(7801002, "Can't mutate accumulated fields after initialization", !_executionStarted);
@@ -117,7 +117,7 @@ boost::intrusive_ptr<Expression> GroupProcessorBase::getIdExpression() const {
 
 void GroupProcessorBase::reset() {
     // Free our resources.
-    _groups = _expCtx->getValueComparator().makeUnorderedValueMap<Accumulators>();
+    _groups = _expCtx->getValueComparator().makeFlatUnorderedValueMap<Accumulators>();
     _memoryTracker.resetCurrent();
 }
 
diff --git a/src/mongo/db/pipeline/group_processor_base.h b/src/mongo/db/pipeline/group_processor_base.h
index 0b45ddf55b8..076e16eaefd 100644
--- a/src/mongo/db/pipeline/group_processor_base.h
+++ b/src/mongo/db/pipeline/group_processor_base.h
@@ -46,7 +46,7 @@ namespace mongo {
 class GroupProcessorBase {
 public:
     using Accumulators = std::vector<boost::intrusive_ptr<AccumulatorState>>;
-    using GroupsMap = ValueUnorderedMap<Accumulators>;
+    using GroupsMap = ValueFlatUnorderedMap<Accumulators>;
 
     GroupProcessorBase(const boost::intrusive_ptr<ExpressionContext>& expCtx,
                        int64_t maxMemoryUsageBytes);
diff --git a/src/mongo/stdx/unordered_map.h b/src/mongo/stdx/unordered_map.h
index ef1ad47d8a6..778fee33f8c 100644
--- a/src/mongo/stdx/unordered_map.h
+++ b/src/mongo/stdx/unordered_map.h
@@ -29,6 +29,7 @@
 
 #pragma once
 
+#include <absl/container/flat_hash_map.h>
 #include <absl/container/node_hash_map.h>
 #include <cstddef>
 
@@ -40,6 +41,10 @@ namespace stdx {
 template <class Key, class Value, class Hasher = DefaultHasher<Key>, typename... Args>
 using unordered_map = absl::node_hash_map<Key, Value, EnsureTrustedHasher<Hasher, Key>, Args...>;
 
+template <class Key, class Value, class Hasher = DefaultHasher<Key>, typename... Args>
+using flat_unordered_map =
+    absl::flat_hash_map<Key, Value, EnsureTrustedHasher<Hasher, Key>, Args...>;
+
 /**
  * Removes the elements from `c` for which `pred(elem)` is true.
  * Returns the count of elements erased.
diff --git a/src/mongo/stdx/unordered_set.h b/src/mongo/stdx/unordered_set.h
index 0eed1a34bde..f4eb64470a9 100644
--- a/src/mongo/stdx/unordered_set.h
+++ b/src/mongo/stdx/unordered_set.h
@@ -31,6 +31,7 @@
 
 #include "mongo/stdx/trusted_hasher.h"
 
+#include <absl/container/flat_hash_set.h>
 #include <absl/container/node_hash_set.h>
 
 namespace mongo {
@@ -39,5 +40,8 @@ namespace stdx {
 template <class Key, class Hasher = DefaultHasher<Key>, typename... Args>
 using unordered_set = absl::node_hash_set<Key, EnsureTrustedHasher<Hasher, Key>, Args...>;
 
+template <class Key, class Hasher = DefaultHasher<Key>, typename... Args>
+using flat_unordered_set = absl::flat_hash_set<Key, EnsureTrustedHasher<Hasher, Key>, Args...>;
+
 }  // namespace stdx
 }  // namespace mongo
