Creates a new MongoLite client instance.
dbPathOrOptions: Either a string path to the SQLite database file (e.g.,'./mydb.sqlite',':memory:') or an options object.MongoLiteOptions:filePath: string — Path to the SQLite database file.verbose?: boolean — (Optional) Enable verbose logging from thesqlite3driver.
Explicitly opens the database connection. Operations will automatically connect if the DB is not already open.
Closes the database connection.
Lists all collections (tables) in the database.
const collections = await client.listCollections().toArray();
console.log('Available collections:', collections);Gets a reference to a collection (table).
name: The name of the collection.- Returns a
MongoLiteCollectioninstance.
Represents a collection and provides methods to interact with its documents. T is a generic type for your document structure, which must extend DocumentWithId (i.e., have an optional _id: string field).
Inserts a single document. If _id is not provided, a UUID will be generated.
- Returns
{ acknowledged: boolean; insertedId: string }.
Inserts multiple documents in a single operation.
- Returns
{ acknowledged: boolean; insertedIds: string[] }.
Finds a single document matching the filter. Returns null if not found.
Finds multiple documents matching the filter. Returns a chainable FindCursor.
Updates a single document matching the filter.
- Returns
{ acknowledged: boolean; matchedCount: number; modifiedCount: number; upsertedId: string | null }.
Updates all documents matching the filter.
Deletes a single document matching the filter.
- Returns
{ acknowledged: boolean; deletedCount: number }.
Deletes all documents matching the filter.
Replaces a single document matching the filter with the provided document.
Finds a document, updates it, and returns the original or updated document.
Finds a document, deletes it, and returns the deleted document.
Finds a document, replaces it, and returns the original or new document.
Returns an array of distinct values for the given field.
Returns the count of documents matching the filter.
Returns a fast estimate of the total document count.
Drops the collection (deletes the underlying table).
Runs an aggregation pipeline. Supported stages: $match, $group, $sort, $limit, $skip, $project, $count, $unwind.
Opens a change stream to watch for changes on this collection.
See CHANGE_STREAMS.md for full documentation.
Returned by collection.find(). Supports chaining before execution.
Fetches all matching documents into an array.
Returns the next document, or null when exhausted.
Limits the number of documents returned.
Skips the first N documents.
Sorts results. Example: { age: -1, name: 1 } (1 = ascending, -1 = descending).
Specifies which fields to include or exclude in results.
Creates an index on the specified field(s).
fieldOrSpec: A field name string or an index spec object, e.g.{ name: 1, age: -1 }.options:unique: Enforce uniqueness.name: Custom index name.sparse: Ignore documents without the indexed field.
- Returns
{ acknowledged: boolean; name: string }.
// Simple index
await collection.createIndex({ name: 1 });
// Unique index
await collection.createIndex({ email: 1 }, { unique: true });
// Compound index
await collection.createIndex({ name: 1, age: -1 });
// Nested field index
await collection.createIndex({ 'address.city': 1 });Lists all indexes on the collection. Each IndexInfo contains name, key, and unique.
Drops the named index. Returns { acknowledged: boolean; name: string }.
Drops all indexes except the one on _id.
| Operator | Description |
|---|---|
{ field: value } or { field: { $eq: value } } |
Equals |
{ field: { $ne: value } } |
Not equals |
{ field: { $gt: value } } |
Greater than |
{ field: { $gte: value } } |
Greater than or equal |
{ field: { $lt: value } } |
Less than |
{ field: { $lte: value } } |
Less than or equal |
{ field: { $in: [v1, v2] } } |
In array |
{ field: { $nin: [v1, v2] } } |
Not in array |
{ field: { $regex: pattern } } |
Matches regular expression |
{ field: { $size: n } } |
Array has N elements |
{ field: { $type: typeName } } |
Field is of given type |
{ field: { $mod: [divisor, remainder] } } |
Modulo |
| Operator | Description |
|---|---|
{ $and: [f1, f2] } |
Logical AND |
{ $or: [f1, f2] } |
Logical OR |
{ $nor: [f1, f2] } |
Logical NOR |
{ field: { $not: expr } } |
Negates a single operator expression |
| Operator | Description |
|---|---|
{ field: { $exists: boolean } } |
Field exists (or not) |
| Operator | Description |
|---|---|
{ field: { $all: [v1, v2] } } |
Array contains all values |
{ field: { $elemMatch: query } } |
At least one element matches the query |
| Operator | Description |
|---|---|
$set |
Sets field values |
$unset |
Removes fields |
$inc |
Increments numeric fields |
$mul |
Multiplies numeric fields |
$min |
Sets field to minimum of current and given value |
$max |
Sets field to maximum of current and given value |
$push |
Appends a value to an array (supports $each) |
$pull |
Removes matching values from an array |
$addToSet |
Adds a value to an array only if it doesn't already exist |
$pop |
Removes the first (-1) or last (1) element of an array |
$currentDate |
Sets a field to the current date |
// $set and $inc
await collection.updateOne({ _id: id }, { $set: { name: 'Bob' }, $inc: { age: 1 } });
// $push with $each
await collection.updateOne({ _id: id }, { $push: { scores: { $each: [90, 92, 85] } } });
// $pull
await collection.updateOne({ _id: id }, { $pull: { scores: 0 } });
// $addToSet
await collection.updateOne({ _id: id }, { $addToSet: { tags: 'new-tag' } });