prequel

As a lonely sandbank cold, single high proportion of the app ape crowd, have to take out the mobile phone shake, see if there is someone interested in nearby. So what happens during the shake?

Make no mistake, get your own location first, and then get your location for the search target.

2dsphere Spherical position index

MongoDB has built-in 2DSphere index, through which distance batch search can be completed. Here’s the test data:

db.location.insert([
 	{name: 'Sanlitun MM1'.gender: 'female'.location: {type: 'Point'.coordinates: [116.458347.39.940602]}}, {name: 'Xizhimen GG1'.gender: 'male'.location: {type: 'Point'.coordinates: [116.361446.39.946471]}}, {name: 'Dongzhimen MM2'.gender: 'female'.location: {type: 'Point'.coordinates: [116.440967.39.945325]}}, {name: 'Tiananmen MM3'.gender: 'female'.location: {type: 'Point'.coordinates: [116.403963.39.915119]}}, {name: 'Sanlitun MM4'.gender: 'female'.location: {type: 'Point'.coordinates: [116.460054.39.938063]}}, {name: 'Sanlitun MM5'.gender: 'female'.location: {type: 'Point'.coordinates: [116.461082.39.944209]}}, {name: 'Sanlitun MM6'.gender: 'female'.location: {type: 'Point'.coordinates: [116.461065.39.939399]}}]);Copy the code

Suppose you were walking down the sanlitun street in Beijing, alone and out of place in the lively nightlife…

In order to pass the lonely time, you pull out your phone, ready to shake someone to find company. Suddenly, a voice in your ear says, “You need to create an index first.” That’s right! Need to have index will be faster, the program ape pay attention to efficiency! , you write the following command:

 db.location.createIndex({location: '2dsphere'});
Copy the code

“That’s not enough. You need to look around, no more than a kilometer away!”

db.location.find(
  {
    location: {
      $near: {
        $geometry: {
          type: 'Point'.coordinates: [116.459958.39.937193]},$maxDistance: 1000}}});Copy the code

Then appeared the potential target around you, as expected found the MM near Sanlitun!

{
	"_id" : ObjectId("60e459140d2f25c251fe0989"),
	"name" : "Sanlitun MM4"."gender" : "female"."location" : {
		"type" : "Point"."coordinates" : [
			116.460054.39.938063]}} {"_id" : ObjectId("60e459140d2f25c251fe098b"),
	"name" : "Sanlitun MM6"."gender" : "female"."location" : {
		"type" : "Point"."coordinates" : [
			116.461065.39.939399]}} {"_id" : ObjectId("60e459140d2f25c251fe0985"),
	"name" : "Sanlitun MM1"."gender" : "female"."location" : {
		"type" : "Point"."coordinates" : [
			116.458347.39.940602]}} {"_id" : ObjectId("60e459140d2f25c251fe098a"),
	"name" : "Sanlitun MM5"."gender" : "female"."location" : {
		"type" : "Point"."coordinates" : [
			116.461082.39.944209]}}Copy the code

You feel a little happy in your heart, but say coldly: “Who said the programmer is not easy to find a mate? With MongoDB, I found a lot of objects without anyone knowing.

“More objects in the code!” The voice next to him began again.

You wake up with a start, gawking at objects on your computer screen. Sanlitun is thousands of kilometers away — we fled Beijing.