Skip to content

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 operators
const bigDeals = await clients
.find({ dealValue: { $gte: 3000 }, status: { $in: ['Proposal', 'Won'] } })
.toArray();
// Text matching: regular expressions work as in MongoDB
const startsWithA = await clients.find({ name: /^a/i }).toArray();
// Either condition
const followUps = await clients
.find({ $or: [{ status: 'Lead' }, { nextFollowUp: { $exists: true } }] })
.toArray();
// Only some fields, sorted, one page at a time
const page = await clients
.find({}, { projection: { name: 1, dealValue: 1 } })
.sort({ dealValue: -1 })
.skip(0)
.limit(2)
.toArray();
// Counting and distinct values
const total = await clients.countDocuments();
const statuses = await clients.distinct('status');
console.log(bigDeals.length, startsWithA.length, followUps.length, page, total, statuses);
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.

  • 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, Date objects 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.
clients.find(filter, { projection: { name: 1, email: 1 } }); // only these fields (and _id)
clients.find(filter).sort({ dealValue: -1, name: 1 }); // 1 ascending, -1 descending
clients.find(filter).skip(40).limit(20); // page 3 of 20

The 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.

await clients.countDocuments({ status: 'Won' });
await clients.estimatedDocumentCount(); // fastest, may be a few seconds behind the app
await clients.distinct('status', { dealValue: { $gt: 0 } });

Made by Faeiz Furqan