MCPcopy
hub / github.com/mastra-ai/mastra / MemoryStorageMongoDB

Class MemoryStorageMongoDB

stores/mongodb/src/storage/domains/memory/index.ts:57–2399  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55import { formatDateForMongoDB } from '../utils';
56
57export class MemoryStorageMongoDB extends MemoryStorage {
58 readonly supportsObservationalMemory = true;
59
60 #connector: MongoDBConnector;
61 #skipDefaultIndexes?: boolean;
62 #indexes?: MongoDBIndexConfig[];
63
64 /** Collections managed by this domain */
65 static readonly MANAGED_COLLECTIONS = [TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, OM_TABLE] as const;
66
67 /**
68 * Retention-eligible collections. The observational-memory collection is
69 * excluded: it has no timestamp anchor to age on. All anchors are BSON dates.
70 */
71 static override readonly retentionTables: RetentionTablesDescriptor = {
72 messages: { table: TABLE_MESSAGES, column: 'createdAt', indexed: true },
73 resources: { table: TABLE_RESOURCES, column: 'createdAt', indexed: true },
74 threads: { table: TABLE_THREADS, column: 'createdAt', indexed: true },
75 };
76
77 constructor(config: MongoDBDomainConfig) {
78 super();
79 this.#connector = resolveMongoDBConfig(config);
80 this.#skipDefaultIndexes = config.skipDefaultIndexes;
81 // Filter indexes to only those for collections managed by this domain
82 this.#indexes = config.indexes?.filter(idx =>
83 (MemoryStorageMongoDB.MANAGED_COLLECTIONS as readonly string[]).includes(idx.collection),
84 );
85 }
86
87 private async getCollection(name: string) {
88 return this.#connector.getCollection(name);
89 }
90
91 async init(): Promise<void> {
92 await this.createDefaultIndexes();
93 await this.createCustomIndexes();
94 }
95
96 /**
97 * Delete memory rows older than each table's `maxAge`, batched. Order is
98 * messages → resources → threads so child rows never outlive the delete of
99 * their thread. Like `deleteThread()`, this does not sweep vector-store
100 * embeddings — semantic-recall vectors live in a separate vector store the
101 * memory domain cannot reach; cleaning those up is the operator's concern.
102 */
103 async prune(policies: Record<string, TableRetentionPolicy>, options?: PruneOptions): Promise<PruneResult[]> {
104 const targets = resolveTargets({
105 policies,
106 descriptor: MemoryStorageMongoDB.retentionTables,
107 order: ['messages', 'resources', 'threads'],
108 });
109 return runPrune({ connector: this.#connector, domain: 'memory', targets, options, logger: this.logger });
110 }
111
112 /**
113 * Returns default index definitions for the memory domain collections.
114 */

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…