Basic Usage
What This Page Covers
After installation and the first connection, use this page to run the common data flow most applications need. The examples follow the usual development order: write and query data first, then paginate, update, cache reads, and decide when to switch to model():
- Insert one or many documents
- Query one document or a list
- Paginate results
- Update, upsert, and delete
- Add a short read cache
- Decide when to switch from
collection()tomodel()
For a runnable version of this flow, open examples/quick-start/basic-operations.ts.
Setup
Insert Data
Use insertOne() for interactive writes and insertMany() when you already have a small batch.
For larger imports, use insertBatch().
Query Data
Use findOne() for one document and find() for lists. find() supports chain helpers such as sort(), limit(), and project().
See findOne(), find(), and chain queries for the full query surface.
Paginate Lists
Use findPage() when a list needs cursor pagination.
Pass after: page.pageInfo.endCursor to load the next page. See findPage() for totals, cursor type hints, and bookmark options.
Update and Upsert
Use updateOne() for a known document and upsertOne() when the document should be created if it does not exist.
See updateOne(), updateMany(), upsertOne(), and the upsert guide.
Count and Delete
Use count() for counters and deleteMany() for cleanup jobs with an explicit filter.
See count(), deleteOne(), and deleteMany().
Cache a Hot Read
Pass cache as a TTL in milliseconds on read operations that can tolerate short-lived cached data.
Collection writes invalidate related query caches after the write succeeds. Cache invalidation is best-effort around database writes, not an atomic database transaction step. See Cache API and Runtime consistency and boundaries.
Collection or Model
Start with collection() when you want MongoDB-native behavior with monSQLize conveniences such as ObjectId conversion, query cache, pagination, transactions, and unified errors.
Switch to model() when writes must pass through schema validation, defaults, hooks, timestamps, soft delete, relations, or optimistic locking.
See Model overview and Write path policy when a service needs to enforce Model-only writes for selected namespaces.
Close the Runtime
Next Steps
- Review constructor options in
configuration.md. - Read the collection API details in
api-index.md. - Compare runnable source files in
examples.md.