Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Array $gte, $gt, $lte, $lt only being cast as number #1101

Closed
wants to merge 1 commit into from

2 participants

@dpatti

This was originally added in 8bdc3d1 for #902. Trying a query similar to this:

model.find({ arr: { $gt: ObjectId("4fc78a59a885233f4b349bd9") } })

Works in the mongo shell, but gives this error in mongoose:

Invalid select() argument. Must be a string or object.

I assume, based on the title, that the problem is that it is being cast as a number instead of using castForQuery as everything else is.

@aheckmann
Owner

odd that the errr message refers to an invalid select. should just bypass casting I guess

@dpatti

No, shit, I'm sorry. That message is not related at all. We were previously using v2 where you could do a select with ['attr1', 'att2'] notation, and that was the error. I just found out a few minutes ago, but I wrote this initial ticket while I did not have access to my code. Sorry for the confusion!

That said, I believe the initial problem is still present.

@aheckmann
Owner

ok, thanks for reporting. i'll be busy for the next week or so. if you have time to submit a patch in the meantime that would be awesome.

@dpatti
@dpatti dpatti Fix query casting for inequalities in arrays
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
5946a71
@dpatti

Let me know if that test is sufficient enough. I didn't think copying and pasting the same thing four times would have been very neat, and I haven't seen any use of test in for loops, so I just went with this.

@aheckmann aheckmann closed this pull request from a commit
@dpatti dpatti Fix query casting for inequalities in arrays
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
10de061
@aheckmann aheckmann closed this in 10de061
@aheckmann
Owner

merged. thanks a bunch!

@aheckmann aheckmann referenced this pull request from a commit
@dpatti dpatti Fix query casting for inequalities in arrays
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
dbda275
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 15, 2012
  1. @dpatti

    Fix query casting for inequalities in arrays

    dpatti authored
    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
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 4 deletions.
  1. +4 −4 lib/schema/array.js
  2. +25 −0 test/query.test.js
View
8 lib/schema/array.js
@@ -211,10 +211,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)
View
25 test/query.test.js
@@ -755,6 +755,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(){
Something went wrong with that request. Please try again.