Skip to content

Commit

Permalink
added; $nearSphere and $polygon support
Browse files Browse the repository at this point in the history
  • Loading branch information
aheckmann committed Jul 25, 2012
1 parent cbed8db commit d054047
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
54 changes: 54 additions & 0 deletions lib/query.js
Expand Up @@ -734,6 +734,31 @@ Query.prototype.near = function (path, val) {
return this;
}

/**
* Specifies a `$nearSphere` condition.
*
* @param {String} path
* @param {Object} val
* @return {Query} this
* @see http://www.mongodb.org/display/DOCS/Geospatial+Indexing
* @api public
*/

Query.prototype.nearSphere = function (path, val) {
if (arguments.length === 1) {
val = path;
path = this._currPath
} else if (arguments.length === 2 && !Array.isArray(val)) {
val = utils.args(arguments);
path = this._currPath;
} else if (arguments.length === 3) {
val = utils.args(arguments, 1);
}
var conds = this._conditions[path] || (this._conditions[path] = {});
conds.$nearSphere = val;
return this;
}

/**
* Specifies a `$mod` condition
*
Expand Down Expand Up @@ -926,6 +951,35 @@ Query.prototype.centerSphere = function (path, val) {
return this;
};

/**
* Specifies a $polygon condition
*
* ####Example
*
* var polyA = [ [ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ] ]
* query.where('loc').within.polygon(polyA)
*
* // or
* var polyB = { a : { x : 10, y : 20 }, b : { x : 15, y : 25 }, c : { x : 20, y : 20 } }
* query.where('loc').within.polygon(polyB)
*
* @param {String} path
* @param {Array|Object} val
* @return {Query} this
* @see http://www.mongodb.org/display/DOCS/Geospatial+Indexing
* @api public
*/

Query.prototype.polygon = function (path, val) {
if (arguments.length === 1) {
val = path;
path = this._currPath;
}
var conds = this._conditions[path] || (this._conditions[path] = {});
conds['$within'] = { '$polygon': val };
return this;
};

/**
* Specifies which document fields to include or exclude
*
Expand Down
49 changes: 49 additions & 0 deletions test/query.test.js
Expand Up @@ -283,6 +283,29 @@ describe('Query', function(){
})
})

describe('nearSphere', function(){
it('via where, where [lat, long] param', function(){
var query = new Query();
query.where('checkin').nearSphere([40, -72]);
assert.deepEqual(query._conditions, {checkin: {$nearSphere: [40, -72]}});
})
it('via where, where lat and long params', function(){
var query = new Query();
query.where('checkin').nearSphere(40, -72);
assert.deepEqual(query._conditions, {checkin: {$nearSphere: [40, -72]}});
})
it('not via where, where [lat, long] param', function(){
var query = new Query();
query.nearSphere('checkin', [40, -72]);
assert.deepEqual(query._conditions, {checkin: {$nearSphere: [40, -72]}});
})
it('not via where, where lat and long params', function(){
var query = new Query();
query.nearSphere('checkin', 40, -72);
assert.deepEqual(query._conditions, {checkin: {$nearSphere: [40, -72]}});
})
})

describe('maxDistance', function(){
it('via where', function(){
var query = new Query();
Expand Down Expand Up @@ -316,6 +339,32 @@ describe('Query', function(){
assert.deepEqual(query._conditions, {gps: {$within: {$center: [[5, 25], 5]}}});
})
})

describe('centerSphere', function(){
it('not via where', function(){
var query = new Query();
query.within.centerSphere('gps', {center: [5, 25], radius: 5});
assert.deepEqual(query._conditions, {gps: {$within: {$centerSphere: [[5, 25], 5]}}});
})
it('via where', function(){
var query = new Query();
query.where('gps').within.centerSphere({center: [5, 25], radius: 5});
assert.deepEqual(query._conditions, {gps: {$within: {$centerSphere: [[5, 25], 5]}}});
})
})

describe('polygon', function(){
it('not via where', function(){
var query = new Query();
query.within.polygon('gps', [[ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ]]);
assert.deepEqual(query._conditions, {gps: {$within: {$polygon:[[ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ]] }}});
})
it('via where', function(){
var query = new Query();
query.where('gps').within.polygon({ a: { x: 10, y: 20 }, b: { x: 15, y: 25 }, c: { x: 20, y: 20 }});
assert.deepEqual(query._conditions, {gps: {$within: {$polygon: { a: { x: 10, y: 20 }, b: { x: 15, y: 25 }, c: { x: 20, y: 20 }} }}});
})
})
})

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

0 comments on commit d054047

Please sign in to comment.