Skip to content

Commit

Permalink
Merge branch '6.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 6, 2023
2 parents 2df20c1 + 02b9b2b commit d4e513d
Show file tree
Hide file tree
Showing 18 changed files with 346 additions and 66 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
6.10.5 / 2023-04-06
===================
* fix(model): execute valid write operations if calling bulkWrite() with ordered: false #13218 #13176
* fix(array): pass-through all parameters #13202 #13201 [hasezoey](https://github.com/hasezoey)
* fix: improve error message when sorting by empty string #13249 #10182
* docs: add version support and check version docs #13251 #13193

5.13.17 / 2023-04-04
====================
* fix: backport fix for array filters handling $or and $and #13195 #13192 #10696 [raj-goguardian](https://github.com/raj-goguardian)
Expand Down
108 changes: 70 additions & 38 deletions benchmarks/get.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,72 @@
'use strict';

const get = require('../lib/helpers/get');

let obj;

// Single string
obj = {};

let start = Date.now();
for (let i = 0; i < 10000000; ++i) {
get(obj, 'test', null);
}
console.log('Single string', Date.now() - start);

// Array of length 1
obj = {};
start = Date.now();
let arr = ['test'];
for (let i = 0; i < 10000000; ++i) {
get(obj, arr, null);
}
console.log('Array of length 1', Date.now() - start);

// String with dots
obj = { a: { b: 1 } };
start = Date.now();
for (let i = 0; i < 10000000; ++i) {
get(obj, 'a.b', null);
}
console.log('String with dots', Date.now() - start);

// Multi element array
obj = { a: { b: 1 } };
start = Date.now();
arr = ['a', 'b'];
for (let i = 0; i < 10000000; ++i) {
get(obj, arr, null);
}
console.log('Multi element array', Date.now() - start);
const mongoose = require('../');

run().catch(err => {
console.error(err);
process.exit(-1);
});

async function run() {
const schema = new mongoose.Schema({
field1: {type: String, default: Math.random().toString(36).slice(2, 16)},
field2: {type: String, default: Math.random().toString(36).slice(2, 16)},
field3: {type: String, default: Math.random().toString(36).slice(2, 16)},
field4: {type: String, default: Math.random().toString(36).slice(2, 16)},
field5: {type: String, default: Math.random().toString(36).slice(2, 16)},
field6: {type: String, default: Math.random().toString(36).slice(2, 16)},
field7: {type: String, default: Math.random().toString(36).slice(2, 16)},
field8: {type: String, default: Math.random().toString(36).slice(2, 16)},
}, { versionKey: false });
const TestModel = mongoose.model('Test', schema);

let tests = [];
for (let i = 0; i < 10000; i++) {
tests.push(new TestModel());
}

let loopStart = Date.now();

// run loop with mongoose objects
for (let k = 0; k < 100; k++) {
for (let test of tests) {
test.field1;
test.field2;
test.field3;
test.field4;
test.field5;
test.field6;
test.field7;
test.field8;
}
}

const results = {
'Model loop ms': Date.now() - loopStart
};

const plainTests = [];
for (let test of tests) {
plainTests.push(test.toObject());
}

loopStart = Date.now();

// run loop with plain objects
for (let k = 0; k < 100; k++) {
for (let test of plainTests) {
test.field1;
test.field2;
test.field3;
test.field4;
test.field5;
test.field6;
test.field7;
test.field8;
}
}

results['POJO loop ms'] = Date.now() - loopStart;

console.log(JSON.stringify(results, null, ' '));
}
34 changes: 34 additions & 0 deletions docs/check-version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# How to Check Your Mongoose Version

To check what version of Mongoose you are using in Node.js, print out the [`mongoose.version` property](./api/mongoose.html#Mongoose.prototype.version) as follows.

```javascript
const mongoose = require('mongoose');

console.log(mongoose.version); // '7.x.x'
```

We recommend printing the Mongoose version from Node.js, because that better handles cases where you have multiple versions of Mongoose installed.
You can also execute the above logic from your terminal using Node.js' `-e` flag as follows.

```
# Prints current Mongoose version, e.g. 7.0.3
node -e "console.log(require('mongoose').version)"
```

## Using `npm list`

You can also [get the installed version of the Mongoose npm package](https://masteringjs.io/tutorials/npm/version) using `npm list`.

```
$ npm list mongoose
test@ /path/to/test
└── mongoose@7.0.3
```

`npm list` is helpful because it can identify if you have multiple versions of Mongoose installed.

Other package managers also support similar functions:

- [`yarn list --pattern "mongoose"`](https://classic.yarnpkg.com/lang/en/docs/cli/list/)
- [`pnpm list "mongoose"`](https://pnpm.io/cli/list)
4 changes: 4 additions & 0 deletions docs/guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ integrating Mongoose with external tools and frameworks.
* [Testing with Jest](jest.html)
* [SSL Connections](tutorials/ssl.html)

## Other Guides

* [How to Check Your Mongoose Version](check-version.html)

## Migration Guides

* [Mongoose 6.x to 7.x](migrating_to_7.html)
Expand Down
2 changes: 2 additions & 0 deletions docs/layout.pug
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ html(lang='en')
a.pure-menu-link(href=`${versions.versionedPath}/docs/migrating_to_7.html`, class=outputUrl === `${versions.versionedPath}/docs/migrating_to_7.html` ? 'selected' : '') Migration Guide
li.pure-menu-item
a.pure-menu-link(href=`${versions.versionedPath}/docs/compatibility.html`, class=outputUrl === `${versions.versionedPath}/docs/compatibility.html` ? 'selected' : '') Version Compatibility
li.pure-menu-item
a.pure-menu-link(href=`${versions.versionedPath}/docs/version-support.html`, class=outputUrl === `${versions.versionedPath}/docs/version-support.html` ? 'selected' : '') Version Support
li.pure-menu-item
a.pure-menu-link(href=`${versions.versionedPath}/docs/faq.html`, class=outputUrl === `${versions.versionedPath}/docs/faq.html` ? 'selected' : '') FAQ
li.pure-menu-item
Expand Down
3 changes: 3 additions & 0 deletions docs/migrating_to_5.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Migrating from 4.x to 5.x

Please note: we plan to discontinue Mongoose 5 support on March 1, 2024.
Please see our [version support guide](./version-support.html).

There are several [backwards-breaking changes](https://github.com/Automattic/mongoose/blob/master/History.md)
you should be aware of when migrating from Mongoose 4.x to Mongoose 5.x.

Expand Down
3 changes: 3 additions & 0 deletions docs/migrating_to_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
}
</style>

Please note: we plan to discontinue Mongoose 5 support on March 1, 2024.
Please see our [version support guide](./version-support.html).

There are several [backwards-breaking changes](https://github.com/Automattic/mongoose/blob/master/CHANGELOG.md)
you should be aware of when migrating from Mongoose 5.x to Mongoose 6.x.

Expand Down
2 changes: 2 additions & 0 deletions docs/source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ docs['docs/jobs.pug'] = {
docs['docs/change-streams.md'] = { title: 'MongoDB Change Streams in NodeJS with Mongoose', markdown: true };
docs['docs/lodash.md'] = { title: 'Using Mongoose with Lodash', markdown: true };
docs['docs/incompatible_packages.md'] = { title: 'Known Incompatible npm Packages', markdown: true };
docs['docs/check-version.md'] = { title: 'How to Check Your Mongoose Version', markdown: true };
docs['docs/version-support.md'] = { title: 'Version Support', markdown: true };

for (const props of Object.values(docs)) {
props.jobs = jobs;
Expand Down
27 changes: 27 additions & 0 deletions docs/version-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Version Support

Mongoose 7.x (released February 27, 2023) is the current Mongoose major version.
We ship all new bug fixes and features to 7.x.

## Mongoose 6

Mongoose 6.x (released August 24, 2021) is currently in legacy support.
We will continue to ship bug fixes to Mongoose 6 until August 24, 2023.
After August 24, 2023, we will only ship security fixes, and backport requested fixes to Mongoose 6.
Please open a [bug report on GitHub](https://github.com/Automattic/mongoose/issues/new?assignees=&labels=&template=bug.yml) to request backporting a fix to Mongoose 6.

We are **not** actively backporting any new features from Mongoose 7 into Mongoose 6.
Until August 24, 2023, we will backport requested features into Mongoose 6; please open a [feature request on GitHub](https://github.com/Automattic/mongoose/issues/new?assignees=&labels=enhancement%2Cnew+feature&template=feature.yml) to request backporting a feature into Mongoose 6.
After August 24, 2023, we will not backport any new features into Mongoose 6.

We do not currently have a formal end of life (EOL) date for Mongoose 6.
However, we will not end support for Mongoose 6 until at least January 1, 2024.

## Mongoose 5

Mongoose 5.x (released January 17, 2018) is currently only receiving security fixes and requested bug fixes.
Please open a [bug report on GitHub](https://github.com/Automattic/mongoose/issues/new?assignees=&labels=&template=bug.yml) to request backporting a fix to Mongoose 5.
We will **not** backport any new features from Mongoose 6 or Mongoose 7 into Mongoose 5.

Mongoose 5.x end of life (EOL) is March 1, 2024.
Mongoose 5.x will no longer receive any updates, security or otherwise, after that date.
39 changes: 30 additions & 9 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const Schema = require('./schema');
const StrictModeError = require('./error/strict');
const ValidationError = require('./error/validation');
const ValidatorError = require('./error/validator');
const VirtualType = require('./virtualtype');
const $__hasIncludedChildren = require('./helpers/projection/hasIncludedChildren');
const applyDefaults = require('./helpers/document/applyDefaults');
const cleanModifiedSubpaths = require('./helpers/document/cleanModifiedSubpaths');
Expand Down Expand Up @@ -722,6 +721,9 @@ Document.prototype.$__init = function(doc, opts) {
function init(self, obj, doc, opts, prefix) {
prefix = prefix || '';

if (obj.$__ != null) {
obj = obj._doc;
}
const keys = Object.keys(obj);
const len = keys.length;
let schemaType;
Expand Down Expand Up @@ -1806,28 +1808,47 @@ Document.prototype.$__setValue = function(path, val) {

Document.prototype.get = function(path, type, options) {
let adhoc;
options = options || {};
if (options == null) {
options = {};
}
if (type) {
adhoc = this.$__schema.interpretAsType(path, type, this.$__schema.options);
}
const noDottedPath = options.noDottedPath;

let schema = this.$__path(path);
// Fast path if we know we're just accessing top-level path on the document:
// just get the schema path, avoid `$__path()` because that does string manipulation
let schema = noDottedPath ? this.$__schema.paths[path] : this.$__path(path);
if (schema == null) {
schema = this.$__schema.virtualpath(path);

if (schema != null) {
return schema.applyGetters(void 0, this);
}
}
if (schema instanceof MixedSchema) {

if (noDottedPath) {
let obj = this._doc[path];
if (adhoc) {
obj = adhoc.cast(obj);
}
if (schema != null && options.getters !== false) {
return schema.applyGetters(obj, this);
}
return obj;
}

if (schema != null && schema.instance === 'Mixed') {
const virtual = this.$__schema.virtualpath(path);
if (virtual != null) {
schema = virtual;
}
}
const pieces = path.indexOf('.') === -1 ? [path] : path.split('.');
let obj = this._doc;

if (schema instanceof VirtualType) {
return schema.applyGetters(void 0, this);
}
const hasDot = path.indexOf('.') !== -1;
let obj = this._doc;

const pieces = hasDot ? path.split('.') : [path];
// Might need to change path for top-level alias
if (typeof this.$__schema.aliases[pieces[0]] === 'string') {
pieces[0] = this.$__schema.aliases[pieces[0]];
Expand Down
12 changes: 11 additions & 1 deletion lib/helpers/document/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const _isEmptyOptions = Object.freeze({
transform: false
});

const noDottedPathGetOptions = Object.freeze({
noDottedPath: true
});

/**
* Compiles schemas.
* @param {Object} tree
Expand Down Expand Up @@ -65,6 +69,7 @@ function defineKey({ prop, subprops, prototype, prefix, options }) {
Document = Document || require('../../document');
const path = (prefix ? prefix + '.' : '') + prop;
prefix = prefix || '';
const useGetOptions = prefix ? Object.freeze({}) : noDottedPathGetOptions;

if (subprops) {
Object.defineProperty(prototype, prop, {
Expand Down Expand Up @@ -189,7 +194,12 @@ function defineKey({ prop, subprops, prototype, prefix, options }) {
enumerable: true,
configurable: true,
get: function() {
return this[getSymbol].call(this.$__[scopeSymbol] || this, path);
return this[getSymbol].call(
this.$__[scopeSymbol] || this,
path,
null,
useGetOptions
);
},
set: function(v) {
this.$set.call(this.$__[scopeSymbol] || this, path, v);
Expand Down

0 comments on commit d4e513d

Please sign in to comment.