Querying
Filters use MongoDB syntax and field API names.
import { DriveDB } from '@faeizfurqan/drivedb';
const db = new DriveDB(process.env.DRIVEDB_URL!).db();const clients = db.collection('clients');
// Comparison and logical operatorsconst bigDeals = await clients .find({ dealValue: { $gte: 3000 }, status: { $in: ['Proposal', 'Won'] } }) .toArray();
// Text matching: regular expressions work as in MongoDBconst startsWithA = await clients.find({ name: /^a/i }).toArray();
// Either conditionconst followUps = await clients .find({ $or: [{ status: 'Lead' }, { nextFollowUp: { $exists: true } }] }) .toArray();
// Only some fields, sorted, one page at a timeconst page = await clients .find({}, { projection: { name: 1, dealValue: 1 } }) .sort({ dealValue: -1 }) .skip(0) .limit(2) .toArray();
// Counting and distinct valuesconst total = await clients.countDocuments();const statuses = await clients.distinct('status');
console.log(bigDeals.length, startsWithA.length, followUps.length, page, total, statuses);Operators
Section titled “Operators”| Kind | Operators |
|---|---|
| Comparison | $eq $ne $gt $gte $lt $lte $in $nin |
| Logical | $and $or $nor $not |
| Element | $exists $type |
| Text | $regex (or a JavaScript RegExp), with $options such as i |
| Arrays | $all $elemMatch $size, and plain equality to match one element |
See the operator reference for the full list.
Fields of each type
Section titled “Fields of each type”- Select fields compare as strings:
{ status: 'Won' }. Multiple select fields are arrays:{ tags: 'urgent' }matches records that have that tag. - Dates are ISO strings. Compare with strings,
Dateobjects or{ $date: … }; values are converted to the field’s precision first, so{ due: { $lt: new Date() } }works. - Link fields hold record ids:
{ client: '<id>' }. - JSON fields can be queried with dot paths:
{ 'address.city': 'Lahore' }. - Empty fields are missing from documents:
{ email: { $exists: false } }finds records without an email.
Projection, sorting, paging
Section titled “Projection, sorting, paging”clients.find(filter, { projection: { name: 1, email: 1 } }); // only these fields (and _id)clients.find(filter).sort({ dealValue: -1, name: 1 }); // 1 ascending, -1 descendingclients.find(filter).skip(40).limit(20); // page 3 of 20The cursor fetches up to 1,000 documents per request and continues as you read, so
toArray() and for await return everything that matches:
import { DriveDB } from '@faeizfurqan/drivedb';
const db = new DriveDB(process.env.DRIVEDB_URL!).db();const clients = db.collection('clients');
// The cursor fetches pages of up to 1,000 documents as you read.let processed = 0;for await (const client of clients.find({}).sort({ name: 1 })) { processed += 1; if (processed === 1) console.log('first:', client.name);}console.log(`processed ${processed} clients`);
// Or page yourself, for example for an API that shows 20 per page.const pageSize = 20;const pageNumber = 1;const results = await clients .find({}) .sort({ _createdAt: -1 }) .skip((pageNumber - 1) * pageSize) .limit(pageSize) .toArray();console.log(`page ${pageNumber}: ${results.length} clients`);Over HTTP, find returns at most limit (≤ 1,000) documents and hasMore; call again with a
larger skip to continue. Sort by a field (or _id) when paging so pages don’t overlap.
Counting and distinct values
Section titled “Counting and distinct values”await clients.countDocuments({ status: 'Won' });await clients.estimatedDocumentCount(); // fastest, may be a few seconds behind the appawait clients.distinct('status', { dealValue: { $gt: 0 } });Made by Faeiz Furqan