运行 读读

查找查找查找查找查找

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

匹配过滤器的文件, 包括传呼、 排序和字段选择 。

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.

参数

collectionstring必填

哪个收藏

filterJSON text

查询 空或空 意味着一切 都与 limit.

options.limitinteger

有多少个? 0 意思是无限制,这很少是你想要的。

options.skipinteger

有多少人可以跨过——这个加 limit 正在呼叫。

options.sortobject

{ field: 1 | -1 }.

options.projectionobject

返回的字段。更少的字段,回答较小。

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": {…}}]
将过滤器发送为 JSON 文字文字文字文字

不是作为对象。 嵌套对象在通过一些呼叫器时会丢失以美元开头的键, 查询会比您想要的更匹配, 无需说明。

身份识别符已被转换为您

A 音 A _id 指定字符串后, 将字符串转换为数据库所期望的标识符类型, 包括内部 $in, $nin 和 $ne。这样你就可以把打印的代号传回去, 而不自己转换。

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]}})

呼叫

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

排序在稳定的东西 - _id 安全默认值。未排序查询的调用可两次显示同一文档,但错失另一文档。

被拒绝的

包含过滤器的过滤器 $where, $function 或 $accumulator 被拒绝:他们在数据库内运行代码。拒绝发生在查询发送之前。