Skip to main content
Version: 0.14.0 (Latest)

🗄️ Mongo

Mongo provides two reusable SDK blocks for MongoDB collection queries and _id exclusion; it is not an HTTP service or a feature set.

Start here

Create or obtain a MongoDB Collection, then call the exported blocks directly or lift them into another domain's route pipeline. The connection helpers are driver exports, documented separately in Database drivers.

There is no mongoService, route, feature, handler, schema, or validator module. findResources accepts an existing MongoDB Collection, a filter/options pair, an error constructor, and an error message; it delegates to collection.find(...).toArray(). buildWithoutMongoIdFindOptions returns { projection: { _id: 0 } } for callers that choose to omit MongoDB's internal ID. getMongoClient and withMongo are separate drivers exports, not Mongo block exports.

import {blocks, drivers, primitives} from '@nodeblocks/backend-sdk';

class ResourceQueryError extends primitives.BlockError {}

const db = drivers.getMongoClient('mongodb://127.0.0.1:27017', 'example');
const locations = db.collection('locations');
const options = blocks.buildWithoutMongoIdFindOptions();
if (options.isErr()) throw options.error;
const result = await blocks.findResources(
locations,
{filter: {type: 'city'}, options: options.value},
ResourceQueryError,
'Unable to find locations',
);

buildWithoutMongoIdFindOptions() cannot fail, so options.value is the projection passed to findResources. Check result.isOk() before consuming the returned documents; on failure, result.error is the supplied error class with the supplied message.

Common tasks

TaskStart withContract
Query a collection with a domain errorfindResourcesfindResources receives { filter, options? }, returns a Result, and creates the caller's supplied BlockError subclass on database failure
Omit Mongo _id from selected documentsbuildWithoutMongoIdFindOptionsbuildWithoutMongoIdFindOptions creates the FindOptions projection; pass its successful value to a query block
Obtain a collection for a service or custom blockDatabase driversDatabase drivers documents getMongoClient and withMongo; service pages document their required dataStores

Reference map

PagePurpose
BlocksThe complete Mongo block reference.

Database drivers own getMongoClient and withMongo. Composition primitives explain applyPayloadArgs, used by cross-domain route pipelines. Error handling explains BlockError and Result handling. The Order, Profile, Notification, and Product blocks wrap findResources with domain-specific error classes.