Zvec Logo

Iterate Over All Documents

Use iter_docs() to stream all documents in a collection one by one.

Unlike fetch() — which retrieves documents by known ids — iteration performs a full scan. Documents are streamed in bounded windows, so the whole collection is never materialized in memory at once — making it suitable for export, backup, migration, or offline processing.

Iterate over all documents
with collection.iter_docs() as docs:
    for doc in docs:
        print(doc.id, doc.fields, doc.vectors)

The iterator holds a native resource slot on the collection, so let the language close it for you: Python's with statement and JavaScript's for...of loop both release the iterator when the loop finishes, breaks early, or raises an exception. Iterators also close themselves automatically when fully exhausted.


Select Fields and Vectors

By default, every scalar field and every vector is materialized for each document. Use output_fields and include_vector to reduce the amount of data read:

  • output_fields (outputFields): a list of scalar field names to return. If omitted, all scalar fields are returned; an empty list returns no scalar fields. Unknown or duplicate names raise an error.
  • include_vector (includeVector): whether to materialize vector data. Vectors are included by default; disable it when you only need scalar fields — skipping vectors makes traversal faster.
Select output fields and exclude vectors
with collection.iter_docs(  
    output_fields=["book_title", "publish_year"],
    include_vector=False,
) as docs:
    for doc in docs:
        print(doc.id, doc.field("book_title"), doc.field("publish_year"))

Snapshot Semantics

Both iter_docs() and iterDocsSync() iterate over an isolated snapshot taken at call time:

  • Data written after the iterator is created — inserts, upserts, updates — is not visible to this iterator.
  • Deletes made after creation do not affect the iteration; documents already deleted when the snapshot was taken are filtered out.
  • On a writable collection, creating the iterator may seal the current writing segment (each call can produce a new small segment); read-only collections are scanned directly without any write.
  • Iteration order is unspecified — it is not insertion order, and it can change after optimize() reorganizes segments — so never rely on it.

Concurrency

While any iterator is open on a collection, these operations raise an error:

  • Schema changes — creating or dropping an index, adding, altering or dropping a column
  • optimize(), which fails at its start
  • Closing or destroying the collection

Conversely, creating an iterator fails while one of those maintenance operations is already running. Writes, flush(), query() and fetch() remain unaffected.

Close every iterator before closing the collection. These restrictions are lifted as soon as all open iterators are closed.


Break Early

Stopping before the end is safe — exiting the with block or the for...of loop releases the iterator's slot. When you drive the iterator manually instead, close it yourself:

Stop iteration early
with collection.iter_docs() as docs:
    for doc in docs:
        if doc.field("publish_year") == 1936:
            print("found:", doc.id)
            break   # the with-block closes the iterator here

On this page