Skip to content

mongodb/client

Documentation

Overview

Classes

DBObject

append()

Appends a key-value pair to the DBObject.

ts
append(key: string, value: any): DBObject;
ParameterTypeDescription
keystringThe field name.
valueanyThe value to append.

Returns

  • Type: DBObject
  • Description: The current DBObject instance for chaining.

toJson()

Converts the DBObject to a standard JavaScript object representation (JSON).

ts
toJson(): void;

Returns

  • Type: void
  • Description: A plain JavaScript object.

markAsPartialObject()

Marks the object as a partial object (used internally by MongoDB driver).

ts
markAsPartialObject(): void;

Returns

  • Type: void
  • Description:

isPartialObject()

Checks if the object is a partial object.

ts
isPartialObject(): boolean;

Returns

  • Type: boolean
  • Description: True if partial, false otherwise.

containsField()

Checks if the DBObject contains a field with the specified key.

ts
containsField(key: string): boolean;
ParameterTypeDescription
keystringThe field name.

Returns

  • Type: boolean
  • Description: True if the field exists, false otherwise.

get()

Gets the value associated with the given key.

ts
get(key: string): any;
ParameterTypeDescription
keystringThe field name.

Returns

  • Type: any
  • Description: The field value.

put()

Puts a key-value pair into the DBObject.

ts
put(key: string, value: any): any;
ParameterTypeDescription
keystringThe field name.
valueanyThe value to put.

Returns

  • Type: any
  • Description: The previous value associated with the key, or null.

removeField()

Removes a field from the DBObject.

ts
removeField(key: string): any;
ParameterTypeDescription
keystringThe field name to remove.

Returns

  • Type: any
  • Description: The removed field value.

Client

getDB()

Retrieves a database instance.

ts
getDB(name: string): DB;
ParameterTypeDescription
namestringOptional name of the database. If not provided, the default database name is used.

Returns

  • Type: DB
  • Description: A DB instance.

DB

getCollection()

Retrieves a collection instance from the database.

ts
getCollection(name: string): DBCollection;
ParameterTypeDescription
namestringThe name of the collection.

Returns

  • Type: DBCollection
  • Description: A DBCollection instance.

DBCollection

insert()

Inserts a document into the collection.

ts
insert(dbObject: DBInput): void;
ParameterTypeDescription
dbObjectDBInputThe document to insert (can be a plain JS object or DBObject).

Returns

  • Type: void
  • Description:

find()

Finds documents matching the query.

ts
find(query: DBInput, projection: DBInput): DBCursor;
ParameterTypeDescription
queryDBInputThe query specification (can be a plain JS object or DBObject).
projectionDBInputThe fields to include or exclude (can be a plain JS object or DBObject).

Returns

  • Type: DBCursor
  • Description: A DBCursor for iterating over results.

findOne()

Finds a single document matching the query.

ts
findOne(query: DBInput, projection: DBInput, sort: DBInput): DBObject;
ParameterTypeDescription
queryDBInputThe query specification.
projectionDBInputThe fields to include or exclude.
sortDBInputThe sorting specification.

Returns

  • Type: DBObject
  • Description: The found document as a DBObject.

findOneById()

Finds a single document by its string ID.

ts
findOneById(id: string, projection: DBInput): DBObject;
ParameterTypeDescription
idstringThe string ID of the document.
projectionDBInputThe fields to include or exclude.

Returns

  • Type: DBObject
  • Description: The found document as a DBObject.

count()

Counts the number of documents in the collection, optionally filtered by a query.

ts
count(query: DBInput): number;
ParameterTypeDescription
queryDBInputOptional query to filter the count.

Returns

  • Type: number
  • Description: The number of documents.

getCount()

Gets the count of documents (alias for count).

ts
getCount(query: DBInput): number;
ParameterTypeDescription
queryDBInputOptional query to filter the count.

Returns

  • Type: number
  • Description: The number of documents.

createIndex()

Creates an index on the collection.

ts
createIndex(keys: DBInput, options: DBInput): void;
ParameterTypeDescription
keysDBInputThe index key specification.
optionsDBInputOptional index options.

Returns

  • Type: void
  • Description:

createIndexForField()

Creates an index on a single field by name.

ts
createIndexForField(name: string): void;
ParameterTypeDescription
namestringThe name of the field to index.

Returns

  • Type: void
  • Description:

distinct()

Retrieves the distinct values for a specified field across a collection. NOTE: The signature in the original code seems slightly off compared to typical MongoDB drivers. This implementation follows the original structure using `keys.native` if `keys` is provided.

ts
distinct(name: string, query: DBInput, keys: DBInput): void;
ParameterTypeDescription
namestringThe field name.
queryDBInputOptional query to filter results.
keysDBInputOptional keys to use for distinct (replaces 'name' if provided and query exists).

Returns

  • Type: void
  • Description:

dropIndex()

Drops a specified index.

ts
dropIndex(index: any): void;
ParameterTypeDescription
indexanyThe name of the index or the DBObject representing the index keys.

Returns

  • Type: void
  • Description:

dropIndexByName()

Drops a specified index by name.

ts
dropIndexByName(name: string): void;
ParameterTypeDescription
namestringThe name of the index.

Returns

  • Type: void
  • Description:

dropIndexes()

Drops all indexes on the collection.

ts
dropIndexes(): void;

Returns

  • Type: void
  • Description:

remove()

Removes documents from the collection matching the query.

ts
remove(query: DBInput): void;
ParameterTypeDescription
queryDBInputThe deletion query specification.

Returns

  • Type: void
  • Description:

rename()

Renames the collection.

ts
rename(newName: string): void;
ParameterTypeDescription
newNamestringThe new name for the collection.

Returns

  • Type: void
  • Description:

save()

Saves a document to the collection. If the document has an `_id`, it performs an update; otherwise, it performs an insert.

ts
save(dbObject: DBInput): void;
ParameterTypeDescription
dbObjectDBInputThe document to save.

Returns

  • Type: void
  • Description:

update()

Updates documents in the collection matching the query.

ts
update(query: DBInput, update: DBInput, upsert: boolean, multi: boolean): void;
ParameterTypeDescription
queryDBInputThe update query specification.
updateDBInputThe update operation specification (e.g., {$set: {...}}).
upsertbooleanIf true, creates a new document if no documents match the query.
multibooleanIf true, updates all documents matching the query; otherwise, only one.

Returns

  • Type: void
  • Description:

updateMulti()

Updates multiple documents in the collection matching the query. (Equivalent to calling `update` with `multi=true` and `upsert=true` implicitly).

ts
updateMulti(query: DBInput, update: DBInput): void;
ParameterTypeDescription
queryDBInputThe update query specification.
updateDBInputThe update operation specification.

Returns

  • Type: void
  • Description:

getNextId()

Calculates the next sequential ID based on the largest existing `_id` in the collection. Assumes `_id` is a numeric field.

ts
getNextId(): number;

Returns

  • Type: number
  • Description: The next available sequential ID (starting at 1 if collection is empty).

generateUUID()

Generates a new random UUID (Universally Unique Identifier).

ts
generateUUID(): string;

Returns

  • Type: string
  • Description: A string representing the UUID.

DBCursor

one()

Returns the single result from the cursor.

ts
one(): DBObject;

Returns

  • Type: DBObject
  • Description: A DBObject representing the document.

batchSize()

Sets the batch size for the cursor.

ts
batchSize(numberOfElements: number): DBCursor;
ParameterTypeDescription
numberOfElementsnumberThe batch size.

Returns

  • Type: DBCursor
  • Description: The DBCursor instance for chaining.

getBatchSize()

Gets the current batch size.

ts
getBatchSize(): number;

Returns

  • Type: number
  • Description: The batch size.

getCollection()

Gets the collection associated with this cursor.

ts
getCollection(): DBCollection;

Returns

  • Type: DBCollection
  • Description: The DBCollection instance.

getCursorId()

Gets the cursor ID.

ts
getCursorId(): string;

Returns

  • Type: string
  • Description: The cursor ID string.

getKeysWanted()

Gets the projection object (fields wanted) used in the query.

ts
getKeysWanted(): DBObject;

Returns

  • Type: DBObject
  • Description: The projection DBObject.

getLimit()

Gets the limit set on the cursor.

ts
getLimit(): number;

Returns

  • Type: number
  • Description: The limit number.

close()

Closes the cursor.

ts
close(): void;

Returns

  • Type: void
  • Description:

hasNext()

Checks if there is a next document in the cursor.

ts
hasNext(): boolean;

Returns

  • Type: boolean
  • Description: True if there is a next document, false otherwise.

next()

Retrieves the next document in the cursor.

ts
next(): DBObject;

Returns

  • Type: DBObject
  • Description: The next document as a DBObject.

getQuery()

Gets the query object used to create this cursor.

ts
getQuery(): DBObject;

Returns

  • Type: DBObject
  • Description: The query DBObject.

length()

Gets the number of documents matched by the query.

ts
length(): number;

Returns

  • Type: number
  • Description: The total number of documents.

sort()

Specifies the order in which the query returns the results.

ts
sort(orderBy: DBInput): DBCursor;
ParameterTypeDescription
orderByDBInputThe sorting specification (e.g., {field: 1} for ascending).

Returns

  • Type: DBCursor
  • Description: The DBCursor instance for chaining.

limit()

Limits the number of results to be returned.

ts
limit(limit: number): DBCursor;
ParameterTypeDescription
limitnumberThe maximum number of documents to return.

Returns

  • Type: DBCursor
  • Description: The DBCursor instance for chaining.

min()

Specifies the exclusive upper bound for a specific index.

ts
min(min: number): DBCursor;
ParameterTypeDescription
minnumberThe minimum value.

Returns

  • Type: DBCursor
  • Description: The DBCursor instance for chaining.

max()

Specifies the exclusive upper bound for a specific index.

ts
max(max: number): DBCursor;
ParameterTypeDescription
maxnumberThe maximum value.

Returns

  • Type: DBCursor
  • Description: The DBCursor instance for chaining.

maxTime()

Sets a timeout for the server to execute the query.

ts
maxTime(maxTime: number): DBCursor;
ParameterTypeDescription
maxTimenumberThe maximum time in milliseconds.

Returns

  • Type: DBCursor
  • Description: The DBCursor instance for chaining.

size()

Gets the size of the result set.

ts
size(): number;

Returns

  • Type: number
  • Description: The size number.

skip()

Skips the specified number of documents.

ts
skip(numberOfElements: number): DBCursor;
ParameterTypeDescription
numberOfElementsnumberThe number of documents to skip.

Returns

  • Type: DBCursor
  • Description: The DBCursor instance for chaining.