Skip to content

Commit

Permalink
fix(document): retain user-defined key order on initial set with nest…
Browse files Browse the repository at this point in the history
…ed docs

Fix #6944
  • Loading branch information
vkarpov15 committed Sep 26, 2018
1 parent fd8e227 commit 9975182
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/document.js
Expand Up @@ -708,6 +708,17 @@ Document.prototype.$set = function $set(path, val, type, options) {
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];
}

if (path[key] !== null &&
path[key] !== void 0 &&
// need to know if plain object - no Buffer, ObjectId, ref, etc
Expand Down
2 changes: 1 addition & 1 deletion test/schema.select.test.js
Expand Up @@ -396,7 +396,7 @@ describe('schema select option', function() {
}), 'gh7017');

return co(function*() {
yield db;
yield db.$initialConnection;
yield db.collection('gh7017').insertOne({
a: 'foo',
b: 'bar',
Expand Down

0 comments on commit 9975182

Please sign in to comment.