Search collection using $near

Hi all,

I’m struggling to figure out how to search a collection and display results by proximity using $near. In my collection schema I have defined an array containing my longitude and latitude, like so:

  geopoint: {
    type: [Number],
    decimal: true,
    optional: true
  }

In the template where I want to display my results, I’m using the following in my helper:

return Therapists.find({
	geopoint: {
		$near: [Session.get('userLng'),Session.get('userLat')], //these values are correctly set
		$maxDistance: 2000
	}
});

This seems to work in principle, but it returns ALL the documents, even those where the two coordinates stored in geopoint are on the other side of the world and regardless of the $maxDistance value. This also happens if I replace the session variables with static values. Am I doing this wrong?

Thanks!

Have you defined a geospatial index, like a 2d Index? (mandatory if you’re using $near)

Thanks. I’ve defined my index as follows:

Therapists._ensureIndex({location:"2dsphere"});

Is that correct? I’m still struggling to wrap my head around all this :-/

No, you are not correct (-:
You geopoint must be a GeoJSON point

Example from my schema:

location: {
	type: Object,
	index: '2dsphere',
	label: 'MongoDB spesific coordinates field'
},
'location.type': {
	type: String,
	allowedValues: ['Point'],
	label: 'Type of coordinates: Point'
},
'locationl.coordinates': {
	type: [Number],
	decimal: true,
	label: 'Array of coordinates in MongoDB style \[Lng, Lat\]'
}

Thanks none. The problem is that when I add this to my schema (correcting the typo in location.coordinates) I get the following error: “MongoError: can’t parse geometry from element:location.”

I’m wondering whether this error maybe has something to do with the fact that I’m using simpleSchema and my schema is being used with autoform? Any thoughts?

Okay, this is really strange…It looks like I get the error if I add your example to my schema OR if I just add:
Therapists._ensureIndex({location:"2dsphere"});

So even if I don’t use your schema I’m still getting the error as soon as I add the _ensureIndex…

In my schema ‘location’ is ‘l’. Try to rename ‘location’ to loc or smth else…maybe ‘location’ is reserved word by MongoDB?

And give us your schema…

Okay, I’ve renamed to loca and it’s not giving errors. However it’s now returning NO users regardless of how big the value of $maxDistance is.

Here’s the schema (it’s long so I’ve abbreviated):

TherapistSchema = new SimpleSchema({
	name: {
		type: String,
		label: "Your name"
	},
	...... LOTS OF STUFF HERE,
	loca: {
		type: Object,
		optional:true,
		index: '2dsphere',
		label: 'MongoDB specific coordinates field'
	},
	'loca.type': {
		type: String,
		allowedValues: ['Point'],
		label: 'Type of coordinates: Point'
	},
	'loca.coordinates': {
		type: [Number],
		decimal: true,
		label: 'Array of coordinates in MongoDB style \[Lng, Lat\]'
	}
});


Therapists.attachSchema(TherapistSchema);  

Here’s how the loca part of a Therapist document looks in the browser console:

Here’s the revised indexing:

Therapists._ensureIndex({loca:"2dsphere"});

And here’s the revised helper:

closetherapists: function(){
	return Therapists.find({
		loca: {
			$near: [Session.get('userLng'),Session.get('userLat')],
			$maxDistance: 20000000
		}
	});
}

Oooh! Wait! It looks like I have it working. I think the issue was that I was using deprecated syntax. I’ve replaced my helper with the following and it works :smile:

 closetherapists: function(){
	return Therapists.find({
		loca: {
			$near : { 
				$geometry : { 
					type : "Point" ,
					coordinates : [Session.get('userLng'),Session.get('userLat')]
				},
				$maxDistance: 2000000
			}
		}
	});
}

Thanks so much for the help none!

Okay, one final question… How would I modify my query to sort the results by proximity…? I’m totally lost on that one.