Mongo geospatial index and Meteor
Asked Answered
W

2

13

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 );
Weighty answered 9/7, 2012 at 9:43 Comment(0)
B
18

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.

Budget answered 24/8, 2012 at 11:5 Comment(3)
.db didn't appear for me right away. I had to wrap my ensureIndex call in a setTimeout to give it time to show up. I'm hazarding a guess that the database is "connecting" and only appears as a key on .mongo once the connection is established. I set it up with a 15 second delay just to be sure, as I don't care too much about subsequent calls -- calling ensureIndex once is all I really needSacrum
Hello, the solution ._driver.mongo.db.ensureIndex does not work anymore with the last Meteor release (0.5). I am getting the following error : "TypeError: Cannot read property 'mongo' of undefined". I don't know where to search in the meteor source to find out a work around. Has anyone an idea ? Thanks in advance. Regards.Weighty
@Weighty See groups.google.com/forum/#!msg/meteor-talk/8PvWXKocIGI/… . As of 0.5, you can just run ._ensureIndex on your collection. So the above example would look: Places._ensureIndex({ loc : "2d" });Freemasonry
C
1

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.

Comparative answered 10/7, 2012 at 3:4 Comment(2)
Hi Jifeng.Yi, thanks for your answer. I have looked at the mongo_driver.js file and I agree with you nothing seems to forbid the use of geospatial mongo index on the server side. I have already tried in a small use case but it does not work (I mean the filter $near is not activated. I get every rows of the collection). I suppose that it is an error in my mongo request. I have to test again. I have been compelled to create my index db.todos.ensureIndex( { loc : "2d" } ) at the database level and not in the startup.js file. regards.Weighty
Hi Jifeng, sorry for my late reply. I didn't get the time to validate my test. Finally, i manage to get it work with the find mongo request and the near option. Now I have to wrap the result in a google map by using handlebar.js. This is another problem... RegardsWeighty

© 2022 - 2024 — McMap. All rights reserved.