I’m attempting to make a geospatial query in mongodb using the $within operator. I have a collection entry that has a location field which holds:
location: {
bounds: {
south_west: { lat: XX.XXXXXX, lng: XX.XXXXX },
north_east: { lat: XX.XXXXXX, lng: XX.XXXXX }
},
center: {
lat: XX.XXXXXX,
lng: XX.XXXXXX
}
}
For my query, I’d like to get all places with a location.center that falls within the bounds of the google.maps object. I have something like this:
query = {}
var southWest = map.getBounds().getSouthWest();
southWest = [southWest.lat(), southWest.lng()];
var northEast = map.getBounds().getNorthEast();
northEast = [northEast.lat(), northEast.lng()];
query["location.center"] = { $within: { $box: [southWest, northEast] } }
But I consistently get
Exception from Meteor.flush: Error: Unrecognized key in selector: $within
at Error (<anonymous>)
at Function.LocalCollection._exprForConstraint (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:559:11)
at Function.LocalCollection._exprForOperatorTest (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:468:36)
at Function.LocalCollection._exprForKeypathPredicate (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:387:34)
at Function.LocalCollection._exprForSelector (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:307:36)
at Function.LocalCollection._compileSelector (http://localhost:3000/packages/minimongo/selector.js?2373a18a9a4640513e41d7850f17f62f76b7c589:284:24)
at new LocalCollection.Cursor (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:71:39)
at LocalCollection.find (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:57:10)
at _.extend.find (http://localhost:3000/packages/mongo-livedata/collection.js?3ef9efcb8726ddf54f58384b2d8f226aaec8fd53:155:34)
at Template.feed.feed_items (http://localhost:3000/client/modules/js/feed.js?5ed30ddbd861944b636dbe03c13cc80421258a5b:129:17)
And I’m unsure how to proceed. With a simple JSON.stringify(query) I’ve noticed that the query is coming out like
{"location.center":{"$within":{"$box":[[39.93623526127148,-75.63687337060549],[39.984129247435064,-75.57438862939455]]}}}
Is there anyway I can prevent the quotes from being wrapped around the $within and $box selectors? I believe that’s where my exception is being thrown from.
The quotes aren’t the problem, that’s just valid JSON. The problem is that minimongo doesn’t implement $within yet. Matt Debergalis commented on Minimongo’s incomplete state (23 oct 2012):
You may have better luck executing this in the server where you have “real” mongodb access, but I’m not sure how that works if the client store doesn’t have the same functionality.
Minimongo’s source code is available. Perhaps you should try your hand at implementing this selector yourself and sending a pull request? 🙂