From a48c4054f38a7a11d968ffb44f7760abb9117ffa Mon Sep 17 00:00:00 2001 From: Aaron Heckmann Date: Thu, 18 Apr 2013 15:27:30 -0700 Subject: [PATCH] fixed; saving populated new docs closes #1442 --- lib/document.js | 3 ++- lib/model.js | 4 ++-- test/document.populate.test.js | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/document.js b/lib/document.js index 9ef9fec6d9a..1b4dc2bddfd 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1427,7 +1427,7 @@ Document.prototype.$__doQueue = function () { */ Document.prototype.toObject = function (options) { - if (options && options.convertToId) { + if (options && options.depopulate && !options.isParent) { // populated paths that we set to a document? Embedded || (Embedded = require('./types/embedded')); if (!(this instanceof Embedded)) { @@ -1444,6 +1444,7 @@ Document.prototype.toObject = function (options) { } ;('minimize' in options) || (options.minimize = this.schema.options.minimize); + options.isParent = false; var ret = clone(this._doc, options); diff --git a/lib/model.js b/lib/model.js index 632811613d8..3a68c7a62f1 100644 --- a/lib/model.js +++ b/lib/model.js @@ -170,7 +170,7 @@ Model.prototype.save = function save (fn) { if (this.isNew) { // send entire doc - var obj = this.toObject({ depopulate: 1 }); + var obj = this.toObject({ depopulate: 1, isParent: 1 }); this.$__version(true, obj); this.collection.insert(obj, options, complete); this.$__reset(); @@ -378,7 +378,7 @@ Model.prototype.$__delta = function () { operand(this, where, delta, data, value); } else { - value = utils.clone(value, { convertToId: 1 }); + value = utils.clone(value, { depopulate: 1 }); operand(this, where, delta, data, value); } } diff --git a/test/document.populate.test.js b/test/document.populate.test.js index 8f0ca7268df..b3ea0ae6442 100644 --- a/test/document.populate.test.js +++ b/test/document.populate.test.js @@ -485,4 +485,22 @@ describe('document.populate', function(){ }) }) }) + + describe('of new document', function(){ + it('should save just the populated _id (gh-1442)', function(done){ + var b = new B({ _creator: user1 }); + b.populate('_creator', function (err, b) { + if (err) return done(err); + assert.equal('Phoenix', b._creator.name); + b.save(function (err) { + assert.ifError(err); + B.collection.findOne({ _id: b._id }, function (err, b) { + assert.ifError(err); + assert.equal(b._creator, String(user1._id)); + done(); + }) + }) + }) + }) + }) });