Skip to content

Commit

Permalink
Merge branch '7.0' into Automatticgh-12872
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 4, 2023
2 parents f069482 + 1cfba03 commit 26d9268
Show file tree
Hide file tree
Showing 91 changed files with 2,441 additions and 2,155 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ jobs:
runs-on: ubuntu-20.04
name: Benchmark TypeScript Types
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
with:
fetch-depth: 0
- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: 16

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ jobs:
runs-on: ubuntu-20.04
name: Test Generating Docs
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0

- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: 16

Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
runs-on: ubuntu-latest
name: Lint JS-Files
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0

- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: 14

Expand Down Expand Up @@ -59,10 +59,10 @@ jobs:
MONGOMS_VERSION: ${{ matrix.mongodb }}
MONGOMS_PREFER_GLOBAL_PATH: 1
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0

- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: ${{ matrix.node }}

Expand Down Expand Up @@ -91,7 +91,7 @@ jobs:
runs-on: ubuntu-20.04
name: Deno tests
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.0.2
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.0.2
- name: Setup
run: |
wget -q https://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.0.tgz
Expand All @@ -103,7 +103,7 @@ jobs:
mongod --version
echo `pwd`/mongodb-linux-x86_64-ubuntu2004-6.0.0/bin >> $GITHUB_PATH
- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: 16
- name: Setup Deno
Expand All @@ -121,9 +121,9 @@ jobs:
runs-on: ubuntu-latest
name: Replica Set tests
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: 16
- run: npm install
Expand All @@ -138,6 +138,6 @@ jobs:
contents: read
steps:
- name: Check out repo
uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
- name: Dependency review
uses: actions/dependency-review-action@v3
4 changes: 2 additions & 2 deletions .github/workflows/tidelift-alignment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
if: github.repository == 'Automattic/mongoose'
steps:
- name: Checkout
uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: 16
- name: Alignment
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/tsd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
runs-on: ubuntu-latest
name: Lint TS-Files
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0

- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: 14

Expand All @@ -40,10 +40,10 @@ jobs:
runs-on: ubuntu-latest
name: Test Typescript Types
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b # v3.2.0
- uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0

- name: Setup node
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: 12

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ coverage
npm-debug.log
data/
.nyc_output/
.env

tools/31*

Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
6.9.0 / 2023-01-25
==================
* feat(schema): add removeVirtual(path) function to schema #12920 [IslandRhythms](https://github.com/IslandRhythms)
* fix(cast): remove empty `$or` conditions after strict applied #12898 [0x0a0d](https://github.com/0x0a0d)
* docs: fixed typo #12946 [Gbengstar](https://github.com/Gbengstar)

6.8.5 / 2023-01-23
==================
* fix(query): correctly pass context when casting $elemMatch #12915 #12909 #12902 [MohOraby](https://github.com/MohOraby)

6.8.4 / 2023-01-17
==================
* fix(collection): handle creating model when connection disconnected with bufferCommands = false #12889
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ await mongoose.connect('mongodb://127.0.0.1/my_database');

Once connected, the `open` event is fired on the `Connection` instance. If you're using `mongoose.connect`, the `Connection` is `mongoose.connection`. Otherwise, `mongoose.createConnection` return value is a `Connection`.
**Note:** _If the local connection fails then try using 127.0.0.1 instead of 127.0.0.1. Sometimes issues may arise when the local hostname has been changed._
**Note:** _If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed._
**Important!** Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc.
Expand Down
50 changes: 48 additions & 2 deletions docs/migrating_to_7.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ you should be aware of when migrating from Mongoose 6.x to Mongoose 7.x.
If you're still on Mongoose 5.x, please read the [Mongoose 5.x to 6.x migration guide](migrating_to_6.html) and upgrade to Mongoose 6.x first.

* [`strictQuery`](#strictquery)
* [Discriminator schemas use base schema options by default](#discriminator-schemas-use-base-schema-options-by-default)
* [Removed `castForQueryWrapper()`, updated `castForQuery()` signature](#removed-castforquerywrapper)

<h3 id="strictquery"><a href="#strictquery">`strictQuery`</a></h3>
<h3 id="strictquery"><a href="#strictquery"><code>strictQuery</code></a></h3>

`strictQuery` is now false by default.

Expand All @@ -25,4 +27,48 @@ const MyModel = mongoose.model('Test', mySchema);
const docs = await MyModel.find({ notInSchema: 1 });
// Empty array in Mongoose 7. In Mongoose 6, this would contain all documents in MyModel
docs;
```
```

<h3 id="discriminator-schemas-use-base-schema-options-by-default"><a href="#discriminator-schemas-use-base-schema-options-by-default">Discriminator schemas use base schema options by default</a></h3>

When you use `Model.discriminator()`, Mongoose will now use the discriminator base schema's options by default.
This means you don't need to explicitly set child schema options to match the base schema's.

```javascript
const baseSchema = Schema({}, { typeKey: '$type' });
const Base = db.model('Base', baseSchema);

// In Mongoose 6.x, the `Base.discriminator()` call would throw because
// no `typeKey` option. In Mongoose 7, Mongoose uses the base schema's
// `typeKey` by default.
const childSchema = new Schema({}, {});
const Test = Base.discriminator('Child', childSchema);

Test.schema.options.typeKey; // '$type'
```

<h3 id="removed-castforquerywrapper"><a href="#removed-castforquerywrapper">Removed <code>castForQueryWrapper</code>, updated <code>castForQuery()</code> signature</a></h3>

Mongoose now always calls SchemaType `castForQuery()` method with 3 arguments: `$conditional`, `value`, and `context`.
If you've implemented a custom schema type that defines its own `castForQuery()` method, you need to update the method as follows.

```javascript
// Mongoose 6.x format:
MySchemaType.prototype.castForQuery = function($conditional, value) {
if (arguments.length === 2) {
// Handle casting value with `$conditional` - $eq, $in, $not, etc.
} else {
value = $conditional;
// Handle casting `value` with no conditional
}
};

// Mongoose 7.x format
MySchemaType.prototype.castForQuery = function($conditional, value, context) {
if ($conditional != null) {
// Handle casting value with `$conditional` - $eq, $in, $not, etc.
} else {
// Handle casting `value` with no conditional
}
};
```
2 changes: 1 addition & 1 deletion docs/populate.md
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ Alternatively, you can check out the [mongoose-autopopulate plugin](http://npmjs
<h3 id="transform-populated-documents"><a href="#transform-populated-documents">Transform populated documents</a></h3>

You can manipulate populated documents using the `transform` option.
If you specify a `transform` function, Mongoose will call this function on every populated document in the result wiwith two arguments: the populated document, and the original id used to populate the document.
If you specify a `transform` function, Mongoose will call this function on every populated document in the result with two arguments: the populated document, and the original id used to populate the document.
This gives you more control over the result of the `populate()` execution.
It is especially useful when you're populating multiple documents.

Expand Down
17 changes: 15 additions & 2 deletions docs/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ If you haven't already, import mongoose:
import mongoose from 'mongoose';
```

To create a transaction, you first need to create a session using or [`Mongoose#startSession`](api/mongoose.html#mongoose_Mongoose-startSession)
To create a transaction, you first need to create a session using [`Mongoose#startSession`](api/mongoose.html#mongoose_Mongoose-startSession)
or [`Connection#startSession()`](api/connection.html#connection_Connection-startSession).

```javascript
Expand Down Expand Up @@ -46,7 +46,20 @@ The changes in that document are not persisted to MongoDB.
The `Connection#transaction()` function informs Mongoose change tracking that the `save()` was rolled back, and marks all fields that were changed in the transaction as modified.

```javascript
[require:transactions.*can save document after aborted transaction]
const doc = new Person({ name: 'Will Riker' });

await db.transaction(async function setRank(session) {
doc.name = 'Captain';
await doc.save({ session });
doc.isNew; // false

// Throw an error to abort the transaction
throw new Error('Oops!');
}, { readPreference: 'primary' }).catch(() => {});

// true, `transaction()` reset the document's state because the
// transaction was aborted.
doc.isNew;
```

<h2 id="with-mongoose-documents-and-save"><a href="#with-mongoose-documents-and-save">With Mongoose Documents and <code>save()</code></a></h2>
Expand Down
23 changes: 0 additions & 23 deletions docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,6 @@ const userSchema = new Schema<IUser>({

That's because `Schema.Types.ObjectId` is a [class that inherits from SchemaType](schematypes.html), **not** the class you use to create a new MongoDB ObjectId.

### Using `extends Document`

Alternatively, your document interface can extend Mongoose's `Document` class.

We **strongly** recommend against using this approach, its support will be dropped in the next major version as it causes major performance issues.
Many Mongoose TypeScript codebases use the below approach.

```typescript
import { Document, Schema, model, connect } from 'mongoose';

interface IUser extends Document {
name: string;
email: string;
avatar?: string;
}
```

This approach works, but we recommend your document interface _not_ extend `Document`.
Using `extends Document` makes it difficult for Mongoose to infer which properties are present on [query filters](queries.html), [lean documents](tutorials/lean.html), and other cases.

We recommend your document interface contain the properties defined in your schema and line up with what your documents look like in MongoDB.
Although you can add [instance methods](guide.html#methods) to your document interface, we do not recommend doing so.

### Using Custom Bindings

If Mongoose's built-in `index.d.ts` file does not work for you, you can remove it in a postinstall script in your `package.json` as shown below.
Expand Down
14 changes: 7 additions & 7 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Before we get into the specifics of validation syntax, please keep the following
Mongoose has several built-in validators.

- All [SchemaTypes](schematypes.html) have the built-in [required](api/schematype.html#schematype_SchemaType-required) validator. The required validator uses the [SchemaType's `checkRequired()` function](api/schematype.html#schematype_SchemaType-checkRequired) to determine if the value satisfies the required validator.
- [Numbers](api/schema-number-js.html#schema-number-js) have [`min` and `max`](schematypes.html#number-validators) validators.
- [Strings](api/schema-string-js.html#schema-string-js) have [`enum`, `match`, `minLength`, and `maxLength`](schematypes.html#string-validators) validators.
- [Numbers](schematypes.html#numbers) have [`min` and `max`](schematypes.html#number-validators) validators.
- [Strings](schematypes.html#strings) have [`enum`, `match`, `minLength`, and `maxLength`](schematypes.html#string-validators) validators.

Each of the validator links above provide more information about how to enable them and customize their error messages.

Expand Down Expand Up @@ -96,7 +96,7 @@ the value `false`, Mongoose will consider that a validation error.

Errors returned after failed validation contain an `errors` object
whose values are `ValidatorError` objects. Each
[ValidatorError](api/error-validation-js.html#error-validation-js) has `kind`, `path`,
[ValidatorError](api/error.html#error_Error-ValidatorError) has `kind`, `path`,
`value`, and `message` properties.
A ValidatorError also may have a `reason` property. If an error was
thrown in the validator, this property will contain the error that was
Expand Down Expand Up @@ -142,10 +142,10 @@ nested objects are not fully fledged paths.
### Update Validators

In the above examples, you learned about document validation. Mongoose also
supports validation for [`update()`](query.html#query_Query-update),
[`updateOne()`](query.html#query_Query-updateOne),
[`updateMany()`](query.html#query_Query-updateMany),
and [`findOneAndUpdate()`](query.html#query_Query-findOneAndUpdate) operations.
supports validation for [`update()`](api/query.html#query_Query-update),
[`updateOne()`](api/query.html#query_Query-updateOne),
[`updateMany()`](api/query.html#query_Query-updateMany),
and [`findOneAndUpdate()`](api/query.html#query_Query-findOneAndUpdate) operations.
Update validators are off by default - you need to specify
the `runValidators` option.

Expand Down
5 changes: 2 additions & 3 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,12 @@ Aggregate.prototype.project = function(arg) {
* #### Example:
*
* aggregate.near({
* near: [40.724, -73.997],
* near: { type: 'Point', coordinates: [40.724, -73.997] },
* distanceField: "dist.calculated", // required
* maxDistance: 0.008,
* query: { type: "public" },
* includeLocs: "dist.location",
* uniqueDocs: true,
* num: 5
* spherical: true,
* });
*
* @see $geoNear https://docs.mongodb.org/manual/reference/aggregation/geoNear/
Expand Down
Loading

0 comments on commit 26d9268

Please sign in to comment.