Is it possible to execute a function in Mongo that accepts any parameters?

Posted by joshua.clayton on Stack Overflow See other posts from Stack Overflow or by joshua.clayton
Published on 2010-06-17T02:08:25Z Indexed on 2010/06/17 2:12 UTC
Read the original article Hit count: 306

Filed under:
|

I'm looking to write a function to do a custom query on a collection in Mongo. Problem is, I want to reuse that function. My thought was this (obviously contrived):

var awesome = function(count) {
  return function() {
    return this.size == parseInt(count);
  };
}

So then I could do something along the lines of:

db.collection.find(awesome(5));

However, I get this error:

error: {
    "$err" : "error on invocation of $where function:
JS Error: ReferenceError: count is not defined nofile_b:1"
}

So, it looks like Mongo isn't honoring scope, but I'm really not sure why. Any insight would be appreciated.

To go into more depth of what I'd like to do:

A collection of documents has lat/lng values, and I want to find all documents within a concave or convex polygon. I have the function written but would ideally be able to reuse the function, so I want to pass in an array of points composing my polygon to the function I execute on Mongo's end. I've looked at Mongo's geospatial querying and it currently on supports circle and box queries - I need something more complex.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about mongodb