Skip to content

Commit

Permalink
fixed; +field conflict with $slice
Browse files Browse the repository at this point in the history
closes #1370
  • Loading branch information
aheckmann committed Apr 3, 2013
1 parent ed12f5d commit ae756ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/query.js
Expand Up @@ -504,7 +504,7 @@ Query.prototype._applyPaths = function applyPaths () {

// if there are other fields being included, add this one
// if no other included fields, leave this out (implied inclusion)
if (false === exclude && keys.length > 1) {
if (false === exclude && keys.length > 1 && !~keys.indexOf(path)) {
fields[path] = 1;
}

Expand Down
75 changes: 47 additions & 28 deletions test/schema.select.test.js
Expand Up @@ -288,40 +288,31 @@ describe('schema select option', function(){
})
})

it('forcing inclusion of a deselected schema path works', function (done) {
var db = start();
var excluded = new Schema({
thin: Boolean
, name: { type: String, select: false }
, docs: [new Schema({ name: { type: String, select: false }, bool: Boolean })]
});

var M = db.model('ForcedInclusionOfPath', excluded);
M.create({ thin: false, name: '1 meter', docs:[{name:'test', bool:false}] }, function (err, d) {
assert.ifError(err);
describe('forcing inclusion of a deselected schema path', function () {
it('works', function (done) {
var db = start();
var excluded = new Schema({
thin: Boolean
, name: { type: String, select: false }
, docs: [new Schema({ name: { type: String, select: false }, bool: Boolean })]
});

M.findById(d)
.select('+name +docs.name')
.exec(function (err, doc) {
var M = db.model('ForcedInclusionOfPath', excluded);
M.create({ thin: false, name: '1 meter', docs:[{name:'test', bool:false}] }, function (err, d) {
assert.ifError(err);
assert.equal(false, doc.thin);
assert.equal('1 meter', doc.name);
assert.equal(false, doc.docs[0].bool);
assert.equal('test', doc.docs[0].name);
assert.equal(d.id, doc.id);

M.findById(d)
.select('+name -thin +docs.name -docs.bool')
.select('+name +docs.name')
.exec(function (err, doc) {
assert.ifError(err);
assert.equal(undefined, doc.thin);
assert.equal(false, doc.thin);
assert.equal('1 meter', doc.name);
assert.equal(undefined, doc.docs[0].bool);
assert.equal(false, doc.docs[0].bool);
assert.equal('test', doc.docs[0].name);
assert.equal(d.id, doc.id);

M.findById(d)
.select('-thin +name -docs.bool +docs.name')
.select('+name -thin +docs.name -docs.bool')
.exec(function (err, doc) {
assert.ifError(err);
assert.equal(undefined, doc.thin);
Expand All @@ -331,21 +322,49 @@ describe('schema select option', function(){
assert.equal(d.id, doc.id);

M.findById(d)
.select('-thin -docs.bool')
.select('-thin +name -docs.bool +docs.name')
.exec(function (err, doc) {
db.close();
assert.ifError(err);
assert.equal(undefined, doc.thin);
assert.equal(undefined, doc.name);
assert.equal('1 meter', doc.name);
assert.equal(undefined, doc.docs[0].bool);
assert.equal(undefined, doc.docs[0].name);
assert.equal('test', doc.docs[0].name);
assert.equal(d.id, doc.id);
done();

M.findById(d)
.select('-thin -docs.bool')
.exec(function (err, doc) {
db.close();
assert.ifError(err);
assert.equal(undefined, doc.thin);
assert.equal(undefined, doc.name);
assert.equal(undefined, doc.docs[0].bool);
assert.equal(undefined, doc.docs[0].name);
assert.equal(d.id, doc.id);
done();
});
});
});
});
});
});

it('works with query.slice (gh-1370)', function(done){
var db = start();
var M = db.model("1370", new Schema({ many: { type: [String], select: false }}));

M.create({ many: ["1", "2", "3", "4", "5"] }, function (err) {
if (err) return done(err);

var query = M.findOne().select("+many").where("many").slice(2);

query.exec(function (err, doc) {
if (err) return done(err);
assert.equal(2, doc.many.length);
done();
});
});
})
});

it('conflicting schematype path selection should not error', function (done) {
Expand Down

0 comments on commit ae756ab

Please sign in to comment.