Skip to content

Commit

Permalink
Merge branch '5.11' into gh-8108
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 19, 2020
2 parents 7447e86 + 5cb7998 commit 47fbf78
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 86 deletions.
4 changes: 2 additions & 2 deletions docs/tutorials/dates.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ new Date('2010'); // 2010-01-01T00:00:00.000Z
Mongoose converts numeric strings that contain numbers outside the [range of representable dates in JavaScript](https://stackoverflow.com/questions/11526504/minimum-and-maximum-date) and converts them to numbers before passing them to the date constructor.

```javascript
require: Date Tutorial.*Example 1.4.3]
[require: Date Tutorial.*Example 1.4.3]
```

## Timezones

[MongoDB stores dates as 64-bit integers](http://bsonspec.org/spec.html), which
means that Mongoose does **not** store timezone information by default. When
you call `Date#toString()`, the JavaScript runtime will use [your OS' timezone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset).
you call `Date#toString()`, the JavaScript runtime will use [your OS' timezone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset).
9 changes: 9 additions & 0 deletions index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ html(lang='en')
<a rel="sponsored" href="https://masonslots.com/">
<img class="sponsor" src="https://images.opencollective.com/masonslots/29251dd/logo/256.png" style="height: 100px">
</a>
<a rel="sponsored" href="https://twojtyp.pl/">
<img class="sponsor" src="https://images.opencollective.com/twojtyp/cd0b486/logo/256.png" style="height: 100px">
</a>
<a rel="sponsored" href="https://nettikasinot247.fi/">
<img class="sponsor" src="https://images.opencollective.com/nettikasinot-24-7/c51fe6a/logo/256.png" style="height: 100px">
</a>
<a rel="sponsored" href="https://www.kasinosivu.com/pikakasinot/">
<img class="sponsor" src="https://images.opencollective.com/pikakasinot/5ef0793/logo/256.png" style="height: 100px">
</a>
</div>
</div>

Expand Down
162 changes: 78 additions & 84 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,11 @@ Document.prototype.$set = function $set(path, val, type, options) {
adhocs[path] = this.schema.interpretAsType(path, type, this.schema.options);
}

if (typeof path !== 'string') {
if (path == null) {
const _ = path;
path = val;
val = _;
} else if (typeof path !== 'string') {
// new Document({ key: val })
if (path instanceof Document) {
if (path.$__isNested) {
Expand All @@ -935,101 +939,91 @@ Document.prototype.$set = function $set(path, val, type, options) {
}
}

if (path == null) {
const _ = path;
path = val;
val = _;
} else {
prefix = val ? val + '.' : '';

keys = Object.keys(path);
const len = keys.length;

// `_skipMinimizeTopLevel` is because we may have deleted the top-level
// nested key to ensure key order.
const _skipMinimizeTopLevel = get(options, '_skipMinimizeTopLevel', false);
if (len === 0 && _skipMinimizeTopLevel) {
delete options._skipMinimizeTopLevel;
if (val) {
this.$set(val, {});
}
return this;
}
prefix = val ? val + '.' : '';

while (i < len) {
_handleIndex.call(this, i++);
}
keys = Object.keys(path);
const len = keys.length;

// `_skipMinimizeTopLevel` is because we may have deleted the top-level
// nested key to ensure key order.
const _skipMinimizeTopLevel = get(options, '_skipMinimizeTopLevel', false);
if (len === 0 && _skipMinimizeTopLevel) {
delete options._skipMinimizeTopLevel;
if (val) {
this.$set(val, {});
}
return this;
}
} else {
this.$__.$setCalled.add(path);
}

function _handleIndex(i) {
key = keys[i];
const pathName = prefix + key;
pathtype = this.schema.pathType(pathName);

// On initial set, delete any nested keys if we're going to overwrite
// them to ensure we keep the user's key order.
if (type === true &&
!prefix &&
path[key] != null &&
pathtype === 'nested' &&
this._doc[key] != null &&
Object.keys(this._doc[key]).length === 0) {
delete this._doc[key];
// Make sure we set `{}` back even if we minimize re: gh-8565
options = Object.assign({}, options, { _skipMinimizeTopLevel: true });
}

if (typeof path[key] === 'object' &&
!utils.isNativeObject(path[key]) &&
!utils.isMongooseType(path[key]) &&
path[key] != null &&
pathtype !== 'virtual' &&
pathtype !== 'real' &&
pathtype !== 'adhocOrUndefined' &&
!(this.$__path(pathName) instanceof MixedSchema) &&
!(this.schema.paths[pathName] &&
this.schema.paths[pathName].options &&
this.schema.paths[pathName].options.ref)) {
this.$__.$setCalled.add(prefix + key);
this.$set(path[key], prefix + key, constructing, options);
} else if (strict) {
// Don't overwrite defaults with undefined keys (gh-3981) (gh-9039)
if (constructing && path[key] === void 0 &&
this.get(pathName) !== void 0) {
return;
for (let i = 0; i < len; ++i) {
key = keys[i];
const pathName = prefix + key;
pathtype = this.schema.pathType(pathName);

// On initial set, delete any nested keys if we're going to overwrite
// them to ensure we keep the user's key order.
if (type === true &&
!prefix &&
path[key] != null &&
pathtype === 'nested' &&
this._doc[key] != null &&
Object.keys(this._doc[key]).length === 0) {
delete this._doc[key];
// Make sure we set `{}` back even if we minimize re: gh-8565
options = Object.assign({}, options, { _skipMinimizeTopLevel: true });
}

if (pathtype === 'adhocOrUndefined') {
pathtype = getEmbeddedDiscriminatorPath(this, pathName, { typeOnly: true });
}
if (typeof path[key] === 'object' &&
!utils.isNativeObject(path[key]) &&
!utils.isMongooseType(path[key]) &&
path[key] != null &&
pathtype !== 'virtual' &&
pathtype !== 'real' &&
pathtype !== 'adhocOrUndefined' &&
!(this.$__path(pathName) instanceof MixedSchema) &&
!(this.schema.paths[pathName] &&
this.schema.paths[pathName].options &&
this.schema.paths[pathName].options.ref)) {
this.$__.$setCalled.add(prefix + key);
this.$set(path[key], prefix + key, constructing, options);
} else if (strict) {
// Don't overwrite defaults with undefined keys (gh-3981) (gh-9039)
if (constructing && path[key] === void 0 &&
this.get(pathName) !== void 0) {
return this;
}

if (pathtype === 'real' || pathtype === 'virtual') {
// Check for setting single embedded schema to document (gh-3535)
let p = path[key];
if (this.schema.paths[pathName] &&
this.schema.paths[pathName].$isSingleNested &&
path[key] instanceof Document) {
p = p.toObject({ virtuals: false, transform: false });
if (pathtype === 'adhocOrUndefined') {
pathtype = getEmbeddedDiscriminatorPath(this, pathName, { typeOnly: true });
}
this.$set(prefix + key, p, constructing, options);
} else if (pathtype === 'nested' && path[key] instanceof Document) {
this.$set(prefix + key,
path[key].toObject({ transform: false }), constructing, options);
} else if (strict === 'throw') {
if (pathtype === 'nested') {
throw new ObjectExpectedError(key, path[key]);
} else {
throw new StrictModeError(key);

if (pathtype === 'real' || pathtype === 'virtual') {
// Check for setting single embedded schema to document (gh-3535)
let p = path[key];
if (this.schema.paths[pathName] &&
this.schema.paths[pathName].$isSingleNested &&
path[key] instanceof Document) {
p = p.toObject({ virtuals: false, transform: false });
}
this.$set(prefix + key, p, constructing, options);
} else if (pathtype === 'nested' && path[key] instanceof Document) {
this.$set(prefix + key,
path[key].toObject({ transform: false }), constructing, options);
} else if (strict === 'throw') {
if (pathtype === 'nested') {
throw new ObjectExpectedError(key, path[key]);
} else {
throw new StrictModeError(key);
}
}
} else if (path[key] !== void 0) {
this.$set(prefix + key, path[key], constructing, options);
}
} else if (path[key] !== void 0) {
this.$set(prefix + key, path[key], constructing, options);
}

return this;
} else {
this.$__.$setCalled.add(path);
}

let pathType = this.schema.pathType(path);
Expand Down

0 comments on commit 47fbf78

Please sign in to comment.