From d2875c2c384a234b77263f7fed78b18d871dc1ac Mon Sep 17 00:00:00 2001 From: Aaron Heckmann Date: Sat, 28 Apr 2012 10:18:52 -0700 Subject: [PATCH] fixed; default application of selected fields closes #870 --- lib/document.js | 10 +++++++--- test/model.querying.test.js | 28 +++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/document.js b/lib/document.js index bf634305520..9869444ce77 100644 --- a/lib/document.js +++ b/lib/document.js @@ -152,9 +152,13 @@ Document.prototype.buildDoc = function (fields) { self._activePaths.default(p); } - } else { - // do nothing. only the fields specified in - // the query will be populated + } else if (p in fields) { + // selected field + def = type.getDefault(self); + if ('undefined' !== typeof def) { + doc_[piece] = def; + self._activePaths.default(p); + } } } else { def = type.getDefault(self); diff --git a/test/model.querying.test.js b/test/model.querying.test.js index 6971cb574dc..6add3445659 100644 --- a/test/model.querying.test.js +++ b/test/model.querying.test.js @@ -612,10 +612,10 @@ module.exports = { BlogPostB.create({title: 'subset 1'}, function (err, created) { should.strictEqual(err, null); BlogPostB.findOne({title: 'subset 1'}, {title: 1, _id: 0}, function (err, found) { + db.close(); should.strictEqual(err, null); should.strictEqual(undefined, found._id); found.title.should.equal('subset 1'); - db.close(); }); }); }, @@ -626,13 +626,13 @@ module.exports = { BlogPostB.create({title: 'subset 1', author: 'me'}, function (err, created) { should.strictEqual(err, null); BlogPostB.find({title: 'subset 1'}, {title: 1, _id: 0}, function (err, found) { + db.close(); should.strictEqual(err, null); should.strictEqual(undefined, found[0]._id); found[0].title.should.equal('subset 1'); should.strictEqual(undefined, found[0].def); should.strictEqual(undefined, found[0].author); should.strictEqual(false, Array.isArray(found[0].comments)); - db.close(); }); }); }, @@ -661,7 +661,6 @@ module.exports = { }); }, - 'exluded fields should be undefined': function () { var db = start() , BlogPostB = db.model('BlogPostB', collection) @@ -709,6 +708,29 @@ module.exports = { }); }, + // gh-870 + 'included fields should have defaults applied when no value exists in db': function () { + var db = start() + , BlogPostB = db.model('BlogPostB', collection) + , id = new DocumentObjectId + + BlogPostB.collection.insert( + { _id: id, title: 'issue 870'}, function (err) { + should.strictEqual(err, null); + + BlogPostB.findById(id, 'def comments', function (err, found) { + db.close(); + should.strictEqual(err, null); + found._id.should.eql(id); + should.strictEqual(undefined, found.title); + should.strictEqual('kandinsky', found.def); + should.strictEqual(undefined, found.author); + should.strictEqual(true, Array.isArray(found.comments)); + found.comments.length.should.equal(0); + }); + }); + }, + 'test for find where partial initialization': function () { var db = start() , BlogPostB = db.model('BlogPostB', collection)