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
[Read concerns](https://www.mongodb.com/docs/manual/reference/read-concern/) are similar to [`writeConcern`](#writeConcern), but for read operations like `find()` and `findOne()`.
1484
+
To set a default `readConcern`, pass the `readConcern` option to the schema constructor as follows.
1485
+
1486
+
```javascript
1487
+
consteventSchema=newmongoose.Schema(
1488
+
{ name:String },
1489
+
{
1490
+
readConcern: { level:'available' } // <-- set default readConcern for all queries
Copy file name to clipboardExpand all lines: docs/transactions.md
+28-3
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# Transactions in Mongoose
2
2
3
-
[Transactions](https://www.mongodb.com/transactions) are new in MongoDB
4
-
4.0 and Mongoose 5.2.0. Transactions let you execute multiple operations
5
-
in isolation and potentially undo all the operations if one of them fails.
3
+
[Transactions](https://www.mongodb.com/transactions) let you execute multiple operations in isolation and potentially undo all the operations if one of them fails.
6
4
This guide will get you started using transactions with Mongoose.
7
5
8
6
<h2id="getting-started-with-transactions"><ahref="#getting-started-with-transactions">Getting Started with Transactions</a></h2>
@@ -86,6 +84,33 @@ Below is an example of executing an aggregation within a transaction.
One major pain point with transactions in Mongoose is that you need to remember to set the `session` option on every operation.
90
+
If you don't, your operation will execute outside of the transaction.
91
+
Mongoose 8.4 is able to set the `session` operation on all operations within a `Connection.prototype.transaction()` executor function using Node's [AsyncLocalStorage API](https://nodejs.org/api/async_context.html#class-asynclocalstorage).
92
+
Set the `transactionAsyncLocalStorage` option using `mongoose.set('transactionAsyncLocalStorage', true)` to enable this feature.
Copy file name to clipboardExpand all lines: docs/typescript/schemas.md
+26-1
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ Mongoose can automatically infer the document type from your schema definition a
9
9
We recommend relying on automatic type inference when defining schemas and models.
10
10
11
11
```typescript
12
-
import { Schema } from'mongoose';
12
+
import { Schema, model } from'mongoose';
13
13
// Schema
14
14
const schema =newSchema({
15
15
name: { type: String, required: true },
@@ -32,6 +32,31 @@ There are a few caveats for using automatic type inference:
32
32
2. You need to define your schema in the `new Schema()` call. Don't assign your schema definition to a temporary variable. Doing something like `const schemaDefinition = { name: String }; const schema = new Schema(schemaDefinition);` will not work.
33
33
3. Mongoose adds `createdAt` and `updatedAt` to your schema if you specify the `timestamps` option in your schema, *except* if you also specify `methods`, `virtuals`, or `statics`. There is a [known issue](https://github.com/Automattic/mongoose/issues/12807) with type inference with timestamps and methods/virtuals/statics options. If you use methods, virtuals, and statics, you're responsible for adding `createdAt` and `updatedAt` to your schema definition.
34
34
35
+
If you need to explicitly get the raw document type (the value returned from `doc.toObject()`, `await Model.findOne().lean()`, etc.) from your schema definition, you can use Mongoose's `inferRawDocType` helper as follows:
36
+
37
+
```ts
38
+
import { Schema, InferRawDocType, model } from'mongoose';
0 commit comments