Skip to content

Commit

Permalink
add $all support to Number
Browse files Browse the repository at this point in the history
  • Loading branch information
aheckmann committed Jul 26, 2011
1 parent 3435b48 commit 666d903
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
10 changes: 6 additions & 4 deletions lib/mongoose/schema/number.js
Expand Up @@ -105,15 +105,17 @@ function handleArray (val) {
}

SchemaNumber.prototype.$conditionalHandlers = {
'$lt': handleSingle
'$lt' : handleSingle
, '$lte': handleSingle
, '$gt': handleSingle
, '$gt' : handleSingle
, '$gte': handleSingle
, '$ne': handleSingle
, '$in': handleArray
, '$ne' : handleSingle
, '$in' : handleArray
, '$nin': handleArray
, '$mod': handleArray
, '$all': handleArray
};

SchemaNumber.prototype.castForQuery = function ($conditional, val) {
var handler;
if (arguments.length === 2) {
Expand Down
56 changes: 40 additions & 16 deletions test/model.querying.test.js
Expand Up @@ -951,21 +951,38 @@ module.exports = {
'test finding where $or': function () {
var db = start()
, Mod = db.model('Mod', 'mods_' + random());
Mod.create({num: 1}, function (err, one) {

Mod.create({num: 1}, {num: 2, str: 'two'}, function (err, one, two) {
should.strictEqual(err, null);
Mod.create({num: 2}, function (err, two) {
should.strictEqual(err, null);
Mod.create({num: 3}, function (err, three) {

var pending = 2;
test1();
test2();

function test1 () {
Mod.find({$or: [{num: 1}, {num: 2}]}, function (err, found) {
done();
should.strictEqual(err, null);
Mod.find({$or: [{num: 1}, {num: 2}]}, function (err, found) {
should.strictEqual(err, null);
found.should.have.length(2);
found[0]._id.should.eql(one._id);
found[1]._id.should.eql(two._id);
db.close();
});
found.should.have.length(2);
found[0]._id.should.eql(one._id);
found[1]._id.should.eql(two._id);
});
});
}

function test2 () {
Mod.find({ $or: [{ str: 'two'}, {str:'three'}] }, function (err, found) {
if (err) console.error(err);
done();
should.strictEqual(err, null);
found.should.have.length(1);
found[0]._id.should.eql(two._id);
});
}

function done () {
if (--pending) return;
db.close();
}
});
},

Expand Down Expand Up @@ -1043,17 +1060,24 @@ module.exports = {
var db = start()
, BlogPostB = db.model('BlogPostB', collection);

BlogPostB.create({numbers: [-1,-2,-3,-4]}, function (err, whereoutZero) {
BlogPostB.create(
{numbers: [-1,-2,-3,-4], meta: { visitors: 4 }}
, {numbers: [0,-1,-2,-3,-4]}
, function (err, whereoutZero, whereZero) {
should.strictEqual(err, null);
BlogPostB.create({numbers: [0,-1,-2,-3,-4]}, function (err, whereZero) {

BlogPostB.find({numbers: {$all: [-1, -2, -3, -4]}}, function (err, found) {
should.strictEqual(err, null);
BlogPostB.find({numbers: {$all: [-1, -2, -3, -4]}}, function (err, found) {
found.should.have.length(2);
BlogPostB.find({'meta.visitors': {$all: [4] }}, function (err, found) {
should.strictEqual(err, null);
found.should.have.length(2);
found.should.have.length(1);
found[0]._id.should.eql(whereoutZero._id);
BlogPostB.find({numbers: {$all: [0, -1]}}, function (err, found) {
db.close();
should.strictEqual(err, null);
found.should.have.length(1);
found[0]._id.should.eql(whereZero._id);
});
});
});
Expand Down

0 comments on commit 666d903

Please sign in to comment.