You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Queries are lazy, so you can build and reuse them before execution. They run only when you `await` them or call a promise method like `.then()`, `.catch()`, or `.finally()`.
543
+
Queries are lazy and immutable — each builder method returns a new query instance, leaving the original unchanged. Queries run only when you `await` them or call a promise method like `.then()`, `.catch()`, or `.finally()`.
541
544
542
545
```ts
543
-
let verifiedUsersQuery =db.collections.users
546
+
// Each builder method returns a new query — the original is never modified.
547
+
const base =db.collections.users
544
548
.find({ isVerified: true })
545
-
.omit({ age: true })
546
-
.sort({ email: "asc" });
549
+
.omit({ age: true });
547
550
548
-
if (limitResults) {
549
-
verifiedUsersQuery=verifiedUsersQuery.limit(10);
550
-
}
551
+
const sorted =base.sort({ email: "asc" }); // new instance
552
+
const limited =base.limit(10); // new instance from base, no sort
551
553
552
-
const verifiedUsers =awaitverifiedUsersQuery;
554
+
const sortedUsers =awaitsorted; // sorted, no limit
555
+
const limitedUsers =awaitlimited; // no sort, limited to 10
0 commit comments