AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { createAction, Property } from '@activepieces/pieces-framework';
import { mongodbAuth } from '../auth';
import { findAndReplaceDocumentsOutputSchema } from '../output-schemas';
import { mongodbApi } from '../common/api';
import { mongodbClient } from '../common/client';
import { mongodbProps } from '../common/props';
export const findAndReplaceDocumentsAction = createAction({
auth: mongodbAuth,
name: 'find_and_replace_documents',
classification: 'WRITE',
displayName: 'Find and Replace Documents',
description: 'Replace documents that match a filter with a new document',
audience: 'human',
outputSchema: findAndReplaceDocumentsOutputSchema,
aiMetadata: { description: 'Replaces the first document matching a filter with an entirely new document via replaceOne (the whole document is overwritten, not patched), and can return either the before or after version. Choose this over Update Documents when swapping a full document rather than applying field-level operators. Filter and replacement are required; supports upsert. Mutating and not idempotent.', idempotent: false },
props: {
database: mongodbProps.database({ required: false }),
collection: mongodbProps.collection({ required: true }),
filter: Property.Json({
displayName: 'Filter',
description: 'MongoDB query to select documents to replace (e.g., {"_id": "123"})',
required: true,
}),
replacement: Property.Json({
displayName: 'Replacement Document',
description: 'New document that will replace the matched documents',
required: true,
}),
upsert: Property.Checkbox({
displayName: 'Upsert',
description: 'Insert the document if no documents match the filter',
required: false,
defaultValue: false,
}),
returnDocument: Property.StaticDropdown({
displayName: 'Return Document',
description: 'Which version of the document to return',
required: false,
defaultValue: 'after',
options: {
options: [
{ label: 'Before Update', value: 'before' },
{ label: 'After Update', value: 'after' },
],
},
}),
},
async run(context) {
const client = await mongodbClient.create({ auth: context.auth });
try {
if (!context.propsValue.collection) {
throw new Error('Collection is required');
}
if (!context.propsValue.filter) {
throw new Error('Filter is required');
}
if (!context.propsValue.replacement) {
throw new Error('Replacement document is required');
}
const databaseName = context.propsValue.database || context.auth.props.database;
if (!databaseName) {
throw new Error('Database is required. Please specify it in the connection settings or in this action.');
}
const db = client.db(databaseName);
const collection = db.collection(context.propsValue.collection);
const filter = context.propsValue.filter;
const replacement = context.propsValue.replacement;
const upsert = context.propsValue.upsert || false;
const returnDocumentPreference = context.propsValue.returnDocument || 'after';
return await mongodbApi.findAndReplaceDocument({
collection,
filter,
replacement,
upsert,
returnDocumentPreference,
});
} finally {
await client.close();
}
},
});