Collection
The Collection
class is an internal class that represents a Glacier Collection.
Properties
name
The name of current collection.
Methods
insertOne
Insert a single document into current collection.
Type
class Collection<TSchema = any> {
insertOne(doc: TSchema): Promise<InsertResult>
}
Example
async function insertOne() {
const now = Date.now()
const result = await myCollection.insertOne({
title: 'test title',
content: 'test content',
createdAt: now,
updatedAt: now
})
console.log(result)
}
updateOne
Update a single document in current collection.
caution
The updateOne
method replaces the value of a field with the specified value, if you specify multiple field-value pairs, it will update or create each field, this is similar to the $set
operator in MongoDB and it will never replaces the entire data record.
Type
class Collection<TSchema = any> {
updateOne(
filter: Filter<TSchema>,
update: Partial<TSchema>
): Promise<UpdateResult>
}
Example
async function updateOne() {
const now = Date.now()
const result = await myCollection.updateOne(
{
_id: '1'
},
{
updatedAt: now
}
)
console.log(result)
}
deleteOne
Delete a document from current collection.
Type
interface DeleteResult {
deletedCount: number
}
class Collection<TSchema = any> {
deleteOne(filter: Filter<TSchema>): Promise<DeleteResult>
}
Example
async function deleteOne() {
const result = await myCollection.deleteOne({
_id: '1'
})
console.log(result)
}
find
Create a cursor for a filter that can be used to iterate over results from current collection.
Return
Type
interface FilterOperators<TValue> {
$eq?: TValue
$gt?: TValue
$gte?: TValue
$in?: Array<TValue>
$lt?: TValue
$lte?: TValue
$ne?: TValue
$nin?: Array<TValue>
$not?: TValue extends string
? FilterOperators<TValue> | RegExp
: FilterOperators<TValue>
$exists?: boolean
$regexp?: string
}
type Filter<TSchema> =
| Partial<WithId<TSchema>>
| (Partial<{
[Property in keyof WithId<TSchema>]:
| FilterOperators<WithId<TSchema>[Property]>
| WithId<TSchema>[Property]
}> &
RootFilterOperators<WithId<TSchema>>)
interface RootFilterOperators<TSchema> {
$and?: Filter<TSchema>[]
$nor?: Filter<TSchema>[]
$or?: Filter<TSchema>[]
$text?: {
$search: string
$language?: string
$caseSensitive?: boolean
$diacriticSensitive?: boolean
}
}
class Collection<TSchema = any> {
find(filter?: Filter<TSchema>): FindCursor<TSchema>
}