Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo geospatial index and Meteor

I am wondering if it is possible to use a mongodb geospatial index with Meteor architecture.

Minimongo does not implement geospatial indices, but does this mean that we cannot use this mongo feature on the server side?

For example, with the todos app, if we use location on the todo, will it be possible to do:

// Publish complete set of lists to all clients.
Meteor.publish('todos', function (lon,lat) {
   return Todos.find({loc: {$near:[lon,lat]}}).limit(2);
});

And on the client side :

Meteor.subscribe('todos', lon, lat );
like image 792
jeangui Avatar asked Jul 09 '12 09:07

jeangui


People also ask

Does MongoDB support geospatial index?

MongoDB provides the following geospatial index types to support the geospatial queries.

What is geospatial indexing?

With the spatio-temporal library, you can use functions to index points within a region, on a region containing points, and points within a radius to enable fast queries on this data during location analysis.

What is 2dsphere index in MongoDB?

A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity. For more information on geospatial queries, see Geospatial Queries.

What is MongoDB sparse index?

Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value. The index skips over any document that is missing the indexed field. The index is "sparse" because it does not include all documents of a collection.


2 Answers

Yes, you can use the MongoDB geospatial index within Meteor, and you can create that index from within your Meteor app too.

- Geospatial Search

I'm using the $within operator below, as opposed to the $near operator mentioned above, but this still applies:

Meteor.publish('places', function(box) {
    return Places.find({ loc : { $within : { $box : box }}});
});

Reminder: These kinds of geo queries are only available on the server (currently).

- Creating a Geospatial Index from within Meteor (rather than in a MongoDB shell)

Places._ensureIndex({ loc : "2d" });

e.g. You could use the above in your bootstrap.js.

Also, you'll probably want to put your ensureIndex in Meteor.startup, or perhaps when you're inserting some initial data.


Warning: As mentioned here, the above method of calling ensureIndex is a work around for want of an official way to call it, so please expect that this might change.

Update: now reflects changes in Meteor 0.5.0, see @Dror's comment below.

like image 148
Darren Shewry Avatar answered Oct 23 '22 12:10

Darren Shewry


Yes, I think you can.

On the server side, Meteor delegates find/update/.. into node-mongo-native call. You can take a look the code in packages/mongo-livedata/mongo_driver.js. And as I know, node-mongo-native supports geospatial index.

like image 20
jifeng.yin Avatar answered Oct 23 '22 10:10

jifeng.yin