Skip to content

Commit

Permalink
fix nested $operators in $all
Browse files Browse the repository at this point in the history
closes #670
  • Loading branch information
aheckmann committed Jan 10, 2012
1 parent 42f5329 commit fb1c31d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/schema/array.js
Expand Up @@ -161,11 +161,23 @@ SchemaArray.prototype.$conditionalHandlers = {
if (!Array.isArray(val)) {
val = [val];
}

val = val.map(function (v) {
if (v && 'Object' === v.constructor.name) {
var o = {};
o[this.path] = v;
var query = new Query(o);
query.cast(this.casterConstructor);
return query._conditions[this.path];
}
return v;
}, this);

return this.castForQuery(val);
}
, '$elemMatch': function (val) {
var query = new Query(val);
query.cast(this.casterConstructor)
query.cast(this.casterConstructor);
return query._conditions;
}
, '$size': function (val) {
Expand Down
25 changes: 25 additions & 0 deletions test/model.querying.test.js
Expand Up @@ -1370,6 +1370,31 @@ module.exports = {
});
},

'find using #all with nested #elemMatch': function () {
var db = start()
, P = db.model('BlogPostB', collection);

var post = new P({ title: "nested elemMatch" });
post.comments.push({ title: 'comment A' }, { title: 'comment B' }, { title: 'comment C' })

var id0 = post.comments[0]._id;
var id1 = post.comments[1]._id;
var id2 = post.comments[2]._id;

post.save(function (err) {
should.strictEqual(null, err);

var query0 = { $elemMatch: { _id: id1, title: 'comment B' }};
var query1 = { $elemMatch: { _id: id2.toString(), title: 'comment C' }};

P.findOne({ comments: { $all: [query0, query1] }}, function (err, p) {
db.close();
should.strictEqual(null, err);
p.id.should.equal(post.id);
});
});
},

'test finding documents where an array of a certain $size': function () {
var db = start()
, BlogPostB = db.model('BlogPostB', collection);
Expand Down

0 comments on commit fb1c31d

Please sign in to comment.