Interface MongoDB Documentation
Overview
Interface MongoDB provides direct access to the underlying MongoDB client instance through a simple, promise-based API. The package exposes a single GetClient() function that returns a mongodb MongoClient, giving you full access to all MongoDB operations including CRUD, aggregations, indexes, and transactions.
Getting Started
Install the package in your project:
npm install @antelopejs/interface-mongodb
GetClient
The GetClient function retrieves the initialized MongoDB client instance. This is the main entry point for all MongoDB operations.
import { GetClient } from "@antelopejs/interface-mongodb";
async function example() {
const client = await GetClient();
// The client is a standard mongodb MongoClient instance
}
The function returns a Promise<MongoClient> that resolves once the connection is established.
CRUD Operations
Use the client to access databases and collections for standard create, read, update, and delete operations.
Insert documents
import { GetClient } from "@antelopejs/interface-mongodb";
async function createUser(name: string, email: string) {
const client = await GetClient();
const db = client.db("myapp");
const collection = db.collection("users");
const result = await collection.insertOne({ name, email, createdAt: new Date() });
return result.insertedId;
}
Find documents
import { GetClient } from "@antelopejs/interface-mongodb";
async function findUsersByRole(role: string) {
const client = await GetClient();
const db = client.db("myapp");
const users = db.collection("users");
return users.find({ role }).toArray();
}
Update documents
import { GetClient } from "@antelopejs/interface-mongodb";
async function updateEmail(userId: string, newEmail: string) {
const client = await GetClient();
const db = client.db("myapp");
const users = db.collection("users");
const { ObjectId } = await import("mongodb");
await users.updateOne(
{ _id: new ObjectId(userId) },
{ $set: { email: newEmail, updatedAt: new Date() } },
);
}
Delete documents
import { GetClient } from "@antelopejs/interface-mongodb";
async function deleteUser(userId: string) {
const client = await GetClient();
const db = client.db("myapp");
const users = db.collection("users");
const { ObjectId } = await import("mongodb");
await users.deleteOne({ _id: new ObjectId(userId) });
}
Aggregation Pipelines
The MongoDB client supports aggregation pipelines for advanced data processing.
import { GetClient } from "@antelopejs/interface-mongodb";
async function getUserCountsByRole() {
const client = await GetClient();
const db = client.db("myapp");
const users = db.collection("users");
return users
.aggregate([
{ $group: { _id: "$role", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
])
.toArray();
}
Indexes
Create indexes to improve query performance.
import { GetClient } from "@antelopejs/interface-mongodb";
async function setupIndexes() {
const client = await GetClient();
const db = client.db("myapp");
const users = db.collection("users");
await users.createIndex({ email: 1 }, { unique: true });
await users.createIndex({ role: 1, createdAt: -1 });
}
For the complete list of available operations, refer to the MongoDB Node.js Driver documentation.