Skip to content

Commit

Permalink
Fix query casting for inequalities in arrays
Browse files Browse the repository at this point in the history
In 8bdc3d1, support for inequalities $gt, $gte, $lt, and $lte were added for
queries involving arrays, but the values given to queries were always cast to
numbers. This change uses castForQuery so that it is cast to the attribute's
type specified in the schema.

closes #1101
  • Loading branch information
dpatti authored and aheckmann committed Sep 18, 2012
1 parent 53a1d58 commit dbda275
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ SchemaArray.prototype.$conditionalHandlers = {
, '$regex': SchemaArray.prototype.castForQuery
, '$near': SchemaArray.prototype.castForQuery
, '$nearSphere': SchemaArray.prototype.castForQuery
, '$gt': castToNumber
, '$gte': castToNumber
, '$lt': castToNumber
, '$lte': castToNumber
, '$gt': SchemaArray.prototype.castForQuery
, '$gte': SchemaArray.prototype.castForQuery
, '$lt': SchemaArray.prototype.castForQuery
, '$lte': SchemaArray.prototype.castForQuery
, '$within': function(val) {
var query = new Query(val);
query.cast(this.casterConstructor)
Expand Down
25 changes: 25 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,31 @@ describe('Query', function(){
assert.deepEqual(params.ids.$elemMatch.$in[0].toString(), ids[0]);
assert.deepEqual(params.ids.$elemMatch.$in[1].toString(), ids[1]);
})

it('inequality operators for an array', function() {
var query = new Query();
var db = start();
var Product = db.model('Product');
var Comment = db.model('Comment');
db.close();

var id = new DocumentObjectId;
var castedComment = { _id: id, text: 'hello there' };
var comment = new Comment(castedComment);

var params = {
ids: { $gt: id }
, comments: { $gt: comment }
, strings: { $gt: 'Hi there' }
, numbers: { $gt: 10000 }
};

query.cast(Product, params);
assert.equal(params.ids.$gt, id);
assert.deepEqual(params.comments.$gt, castedComment);
assert.equal(params.strings.$gt, 'Hi there');
assert.equal(params.numbers.$gt, 10000);
})
})

describe('distinct', function(){
Expand Down

0 comments on commit dbda275

Please sign in to comment.