run · read

find

storage.run({ action: "find", collection, filter, options })

Documents matching a filter, with paging, sorting and a choice of fields.

document databasefreefilter as JSON text
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Parameters

collectionstringrequired

Which collection.

filterJSON text

The query. Omitted or empty means everything — bound it with limit.

options.limitinteger

How many. 0 means no limit, which is rarely what you want.

options.skipinteger

How many to step over — this plus limit is paging.

options.sortobject

{ field: 1 | -1 }.

options.projectionobject

Which fields to return. Fewer fields, smaller answer.

await storage.run({
  action: "find",
  collection: "orders",
  filter: JSON.stringify({ status: "paid", amount: { $gt: 100 } }),
  options: { limit: 20, skip: 0, sort: { createdAt: -1 },
             projection: { _id: 1, amount: 1, customer: 1 } },
});
// [{ _id: "…", amount: 240, customer: { … } }]
import json

storage.run(
    action="find",
    collection="orders",
    filter=json.dumps({"status": "paid", "amount": {"$gt": 100}}),
    options={"limit": 20, "skip": 0, "sort": {"createdAt": -1},
             "projection": {"_id": 1, "amount": 1, "customer": 1}},
)
# [{"_id": "…", "amount": 240, "customer": {…}}]
Send the filter as JSON text

Not as an object. A nested object loses keys beginning with $ on the way through some callers, and the query then matches more than you meant, without saying so.

Identifiers are converted for you

An _id given as a string is turned into the identifier type the database expects, including inside $in, $nin and $ne. So you can pass back an id you printed, without converting it yourself.

filter: JSON.stringify({ _id: "66f0c1a2b3c4d5e6f7a8b9c0" })
filter: JSON.stringify({ _id: { $in: [idA, idB] } })
filter = json.dumps({"_id": "66f0c1a2b3c4d5e6f7a8b9c0"})
filter = json.dumps({"_id": {"$in": [id_a, id_b]}})

Paging

async function* pages(storage, collection, filter, size = 100) {
  for (let skip = 0; ; skip += size) {
    const rows = await storage.run({ action: "find", collection, filter,
      options: { limit: size, skip, sort: { _id: 1 } } });
    if (!rows.length) return;
    yield rows;
    if (rows.length < size) return;
  }
}
def pages(storage, collection, filter, size=100):
    skip = 0
    while True:
        rows = storage.run(action="find", collection=collection, filter=filter,
                           options={"limit": size, "skip": skip, "sort": {"_id": 1}})
        if not rows:
            return
        yield rows
        if len(rows) < size:
            return
        skip += size

Sort on something stable — _id is the safe default. Paging an unsorted query can show you the same document twice and miss another.

What is refused

A filter containing $where, $function or $accumulator is refused: they run code inside the database. The refusal happens before the query is sent.