v0.6.0-rc.1
HomeDocumentationWorking with data

Ask better questions.

Use MongoDB 4.0-compatible filters, projections and cursor operations.

Comparison and logical operators

javascript
const users = BsonLite.collection('users');

const matches = await users.find({
  active: true,
  age: { $gte: 18, $lt: 65 },
  role: { $in: ['maker', 'designer'] }
}).toArray();

const either = await users.find({
  $or: [{ city: 'Dhaka' }, { remote: true }]
}).toArray();

Fields in a filter are combined with an implicit AND. Comparison operators include $eq, $ne, $gt, $gte, $lt, $lte, $in and $nin. Use $and, $or and $nor for explicit logical composition.

Nested fields and arrays

javascript
const products = BsonLite.collection('products');

const selected = await products.find({
  'details.color': 'green',
  tags: { $all: ['portable', 'offline'] },
  variants: { $elemMatch: { size: 'M', stock: { $gt: 0 } } }
}).toArray();

Use dot notation for nested fields. $elemMatch makes conditions apply to the same array element. Scalar fields and array contents remain BSON values in the engine; encryption is handled by the storage layer.

Sort and paginate

javascript
const page = 1;
const pageSize = 20;

const records = await users.find({}, { projection: { name: 1, role: 1 } })
  .sort({ name: 1, _id: 1 })
  .skip((page - 1) * pageSize)
  .limit(pageSize)
  .toArray();

Use a stable sort with an _id tie-breaker. For large offsets, consider an indexed cursor-based approach. Add indexes that reflect the filters and sort patterns you actually use.

Know the server boundary

MongoDB 4.0 syntax

The SDK uses a MongoDB 6.16 driver against a custom MongoDB 4.0.28 server. A newer driver does not add newer server operators. Server-side JavaScript, including $where and JavaScript mapReduce, is disabled. Newer operators such as $setWindowFields and $function are outside this profile.

The Query library contains copyable examples and explicitly illustrative output. It does not start a database inside your browser.

Documentation for BsonLite 0.6.0-rc.1. View SDK source · Download the validated SDK