Class
SinterCollection
A collection of documents, typed by TDocument.
Get one from SinterDatabase.collection. The type parameter makes filters, updates and results type-checked against your document shape; it is not enforced by the server.
class SinterCollection<TDocument extends object = Document>Defined in packages/driver/src/collection.ts:138
Constructors#
constructor
#new SinterCollection<TDocument extends object = Document>(
database: SinterDatabase,
name: string,
)Parameters
database: SinterDatabaseThe database this collection belongs to.
name: string
Defined in packages/driver/src/collection.ts:145
Properties#
database
#readonly database: SinterDatabaseThe database this collection belongs to.
Defined in packages/driver/src/collection.ts:147
name
#readonly name: stringThe collection name.
Defined in packages/driver/src/collection.ts:143
Accessors#
namespace
#get namespace(): stringThe database and collection names joined with a dot, such as app.users.
Defined in packages/driver/src/collection.ts:157
Methods#
aggregate
#aggregate<TResult extends object = Document>(
pipeline: readonly AggregationStage<TDocument>[],
options?: AggregateOptions,
): AggregateCursor<TResult>Starts an aggregation and returns a cursor. No request is sent until the cursor is read. See AggregationStage for the stages.
The result type is not worked out from the pipeline: pass it as the type
argument when you need it, such as aggregate<{ total: number }>(...).
Example
const [{ total }] = await users
.aggregate<{ total: number }>([
{ $match: { active: true } },
{ $count: "total" },
])
.toArray();
Defined in packages/driver/src/collection.ts:471
countDocuments
#countDocuments(
filter?: Filter<TDocument>,
options?: QueryBudgetOptions,
): Promise<number>Counts the documents that match the filter, on the server, without transferring them. Without a filter it counts the whole collection. A collection that does not exist has no documents.
Defined in packages/driver/src/collection.ts:248
createIndex
#createIndex(definition: IndexDefinition<TDocument>): Promise<CreateIndexResult>Creates an index, or confirms that an identical one exists. The collection
is created if it does not exist. A unique index fails with a
DuplicateKey server error when existing documents already violate it.
Defined in packages/driver/src/collection.ts:367
deleteMany
#deleteMany(filter: Filter<TDocument>): Promise<DeleteResult>Deletes every document that matches the filter.
Defined in packages/driver/src/collection.ts:298
deleteOne
#deleteOne(filter: Filter<TDocument>): Promise<DeleteResult>Deletes the first document that matches the filter.
Defined in packages/driver/src/collection.ts:293
distinct
#distinct<TField extends string>(
field: TField,
filter?: Filter<TDocument>,
options?: QueryBudgetOptions,
): Promise<DistinctValue<TDocument, TField>[]>Returns the distinct values of a field among the documents that match the
filter, in the order described in docs/query-semantics.md.
A field that holds an array contributes its elements, and a document
without the field contributes nothing. Values are distinct by exact
equality, so 1 and 1n are two values.
Defined in packages/driver/src/collection.ts:277
dropIndex
#dropIndex(name: string): Promise<void>Drops an index by name. The _id index cannot be dropped.
Defined in packages/driver/src/collection.ts:394
estimatedDocumentCount
#estimatedDocumentCount(): Promise<number>Counts every document in the collection. It needs no scan: the server answers from the size of the collection.
Defined in packages/driver/src/collection.ts:265
find
#find<const TProjection extends Projection<TDocument> | undefined = undefined>(
filter?: Filter<TDocument>,
options?: FindOptions<TDocument, TProjection>,
): FindCursor<TDocument, ProjectedDocument<TDocument, TProjection>>Starts a query and returns a cursor. No request is sent until the cursor is read.
The cursor can be read with next(), collected with toArray(), or
iterated with for await. Always finish or close() it so the server
can release it.
Defined in packages/driver/src/collection.ts:431
findOne
#findOne<const TProjection extends Projection<TDocument> | undefined = undefined>(
filter?: Filter<TDocument>,
options?: FindOneOptions<TDocument, TProjection>,
): Promise<ProjectedDocument<TDocument, TProjection> | null>Finds the first document that matches the filter, or null. Without a
filter, it returns the first document in the collection.
Defined in packages/driver/src/collection.ts:222
indexes
#indexes(): Promise<IndexInfo[]>The _id index first, then the others in creation order.
Defined in packages/driver/src/collection.ts:402
insertMany
#insertMany(
documents: readonly OptionalId<TDocument>[],
): Promise<InsertManyResult>Inserts several documents in order.
An empty array is rejected. If one document fails, the documents before
it stay inserted, and the thrown SinterInsertManyError reports
failedIndex and the insertedIds that were committed.
Returns Promise<InsertManyResult>
The number of documents inserted and their _id values.
Defined in packages/driver/src/collection.ts:195
insertOne
#insertOne(document: OptionalId<TDocument>): Promise<InsertOneResult>Inserts one document.
The collection is created if it does not exist. A missing _id is
generated.
Throws
SinterServerError with a DuplicateKey name when _id
or a unique index value already exists.
Returns Promise<InsertOneResult>
The _id of the inserted document.
Defined in packages/driver/src/collection.ts:171
replaceOne
#replaceOne(
filter: Filter<TDocument>,
replacement: OptionalId<TDocument>,
options?: UpdateOptions,
): Promise<UpdateResult>Replaces the first matching document with replacement.
The document keeps its _id. Giving the replacement a different _id
fails with an ImmutableId server error. With upsert, a replacement is
inserted when nothing matches.
Defined in packages/driver/src/collection.ts:309
updateMany
#updateMany(
filter: Filter<TDocument>,
update: UpdateFilter<TDocument>,
options?: UpdateOptions,
): Promise<UpdateResult>Applies an update to every matching document.
With upsert, a document seeded from the equality terms of the filter is
inserted, updated, and reported in upsertedId when nothing matches.
Returns Promise<UpdateResult>
modifiedCount counts only documents whose stored bytes
actually changed.
Defined in packages/driver/src/collection.ts:354
updateOne
#updateOne(
filter: Filter<TDocument>,
update: UpdateFilter<TDocument>,
options?: UpdateOptions,
): Promise<UpdateResult>Applies an update to the first matching document.
With upsert, a document seeded from the equality terms of the filter is
inserted, updated, and reported in upsertedId when nothing matches.
Returns Promise<UpdateResult>
modifiedCount counts only documents whose stored bytes
actually changed.
Defined in packages/driver/src/collection.ts:337
validateIndexes
#validateIndexes(): Promise<IndexValidationResult>Asks the server to rebuild every index and report any difference.
Defined in packages/driver/src/collection.ts:413