Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(document): allow setting keys with dots in mixed paths underneath nested paths #13536

Merged
merged 4 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ You can connect to MongoDB with the `mongoose.connect()` method.
mongoose.connect('mongodb://127.0.0.1:27017/myapp');
```

This is the minimum needed to connect the `myapp` database running locally
on the default port (27017). If connecting fails on your machine, try using
`127.0.0.1` instead of `localhost`.
This is the minimum needed to connect the `myapp` database running locally on the default port (27017).
For local MongoDB databases, we recommend using `127.0.0.1` instead of `localhost`.
That is because Node.js 18 and up prefer IPv6 addresses, which means, on many machines, Node.js will resolve `localhost` to the IPv6 address `::1` and Mongoose will be unable to connect, unless the mongodb instance is running with ipv6 enabled.

You can also specify several more parameters in the `uri`:

Expand Down
13 changes: 13 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ hr {
}
</style>

<hr id="localhost-ipv6" />

<a class="anchor" href="#localhost-ipv6">**Q**</a>. I get an error `connect ECONNREFUSED ::1:27017` when connecting to `localhost`. Why?

The easy solution is to replace `localhost` with `127.0.0.1`.

The reason why this error happens is that Node.js 18 and up prefer IPv6 addresses over IPv4 by default.
And, most Linux and OSX machines have a `::1 localhost` entry in `/etc/hosts` by default.
That means that Node.js 18 will assume that `localhost` means the IPv6 `::1` address.
And MongoDB doesn't accept IPv6 connections by default.

You can also fix this error by [enabling IPv6 support on your MongoDB server](https://www.mongodb.com/docs/manual/core/security-mongodb-configuration/).

<hr id="operation-buffering-timed-out" />

<a class="anchor" href="#operation-buffering-timed-out">**Q**</a>. Operation `...` timed out after 10000 ms. What gives?
Expand Down
3 changes: 0 additions & 3 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const clone = require('./helpers/clone');
const compile = require('./helpers/document/compile').compile;
const defineKey = require('./helpers/document/compile').defineKey;
const flatten = require('./helpers/common').flatten;
const flattenObjectWithDottedPaths = require('./helpers/path/flattenObjectWithDottedPaths');
const get = require('./helpers/get');
const getEmbeddedDiscriminatorPath = require('./helpers/document/getEmbeddedDiscriminatorPath');
const getKeysInSchemaOrder = require('./helpers/schema/getKeysInSchemaOrder');
Expand Down Expand Up @@ -473,8 +472,6 @@ function $applyDefaultsToNested(val, path, doc) {
return;
}

flattenObjectWithDottedPaths(val);

const paths = Object.keys(doc.$__schema.paths);
const plen = paths.length;

Expand Down
39 changes: 0 additions & 39 deletions lib/helpers/path/flattenObjectWithDottedPaths.js

This file was deleted.

21 changes: 21 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12212,6 +12212,27 @@ describe('document', function() {
const fromDb = await Test.findById(x._id).lean();
assert.equal(fromDb.c.x.y, 1);
});

it('should allow storing keys with dots in name in mixed under nested (gh-13530)', async function() {
const TestModelSchema = new mongoose.Schema({
metadata:
{
labels: mongoose.Schema.Types.Mixed
}
});
const TestModel = db.model('Test', TestModelSchema);
const { _id } = await TestModel.create({
metadata: {
labels: { 'my.label.com': 'true' }
}
});
const doc = await TestModel.findById(_id).lean();
assert.deepStrictEqual(doc.metadata, {
labels: {
'my.label.com': 'true'
}
});
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down