mingo
Version:
MongoDB query language for in-memory objects
111 lines (110 loc) • 4.65 kB
TypeScript
import { SingleKeyRecord } from "./operators/update/_internal";
import { Any, AnyObject, CollationSpec, Criteria, Options, UpdateExpr } from "./types";
export type PipelineStage = {
$addFields: AnyObject;
} | {
$set: AnyObject;
} | {
$project: AnyObject;
} | {
$unset: string | string[];
} | {
$replaceRoot: {
newRoot: AnyObject;
};
} | {
$replaceWith: AnyObject;
};
export interface Modifier<T> {
$addToSet?: UpdateExpr<T>;
$bit?: UpdateExpr<T, SingleKeyRecord<"and" | "or" | "xor", number>>;
$currentDate?: UpdateExpr<T, true | {
$type: "date" | "timestamp";
}>;
$inc?: UpdateExpr<T, number>;
$max?: UpdateExpr<T>;
$min?: UpdateExpr<T>;
$mul?: UpdateExpr<T, number>;
$pop?: UpdateExpr<T, 1 | -1>;
$pull?: UpdateExpr<T>;
$pullAll?: UpdateExpr<T, Any[]>;
$push?: UpdateExpr<T>;
$rename?: UpdateExpr<T, string>;
$set?: UpdateExpr<T>;
$unset?: UpdateExpr<T, "">;
}
/**
* Supported cloning modes.
* - "deep": Performs a recursive deep clone of the object.
* - "copy": Performs a shallow copy of the object. @default
* - "none": No cloning. Uses the value as given. NOT RECOMMENDED.
*/
export type CloneMode = "deep" | "copy" | "none";
/** Extra configuration to customize the update operation */
export interface UpdateConfig {
/** An array of filter documents that determine which array elements to modify for an update operation on an array field. */
arrayFilters?: AnyObject[];
/** Determines how to set values to fields. */
cloneMode?: CloneMode;
/** {@link updateOne} updates the first document in the sort order specified by this argument. */
sort?: Record<string, 1 | -1>;
/** The collation to use for the operation. Merged into {@link Options.collation} when specified. */
collation?: CollationSpec;
/** A document with a list of variables. Merged into {@link Options.variables} when specified. */
let?: AnyObject;
}
/**
* Updates the given object with the expression.
*
* @param obj The object to update.
* @param modifier The modifications to apply.
* @param arrayFilters Filters to apply to nested items.
* @param condition Conditions to validate before performing update.
* @param options Update options to override defaults.
* @returns {string[]} A list of modified field paths in the object.
*/
export declare function update<T extends AnyObject>(obj: T, modifier: Modifier<T>, arrayFilters?: AnyObject[], condition?: Criteria<T>, options?: {
cloneMode?: CloneMode;
queryOptions?: Partial<Options>;
}): string[];
/**
* Updates all documents that match the specified filter for a collection.
*
* Supports both aggregation pipeline updates and standard update operators.
* Documents in the collection may be replaced or modified.
*
* @param documents - The array of documents to update.
* @param condition - The selection criteria for the update.
* @param modifier - The modifications to apply.
* @param updateConfig - Optional update config parameters.
* @param options - Optional settings to control update behavior.
*/
export declare function updateMany<T extends AnyObject>(documents: T[], condition: Criteria<T>, modifier: Modifier<T> | PipelineStage[], updateConfig?: UpdateConfig, options?: Partial<Options>): {
matchedCount: number;
modifiedCount: number;
};
/**
* Updates a single document within the collection based on the filter.
*
* Supports both aggregation pipeline updates and standard update operators.
* Returns the number of documents matched and modified.
* Objects in the array may be modified inplace or replaced entirely.
*
* @param documents - The array of documents to update.
* @param condition - The selection criteria for the update.
* @param modifier - The modifications to apply.
* @param updateConfig - Optional update config parameters.
* @param options - Optional settings to control update behavior.
*/
export declare function updateOne<T extends AnyObject>(documents: T[], condition: Criteria<T>, modifier: Modifier<T> | PipelineStage[], updateConfig?: UpdateConfig, options?: Partial<Options>): UpdateResult;
/** Result of update operation */
export interface UpdateResult {
/** Count of objects that matched filter. */
readonly matchedCount: number;
/** Count of objects modified. */
readonly modifiedCount: number;
/** Array of modified fields of single object. Available only for {@link updateOne}. */
readonly modifiedFields?: string[];
/** Index of the modified object within the collection. Available only for {@link updateOne}. */
readonly modifiedIndex?: number;
}