Problems with $ next to MongoDB without using $ maxDistance

We currently have big problems with operation $nearc MongoDB 3.2.9.

The problem is that when creating the cursor there is an infinite load - up to the timeout. This problem arises only when we do not use $maxDistance- which in our case is important not to use.

This is our collection:

Places

About 2,000,000 documents with GeoJSON geometry, which is also indexed.

In addition to geometry, there is another index called the "category".

In our special case, our request is as follows:

Locations.find({
  category: 'ABC',
  geometry: { 
    '$near': { 
      $geometry: {
        type: "Point" ,
        coordinates: [ 13.357315063476564, 52.53167855932515 ]
      }
    } 
  } 
}, {
  limit: 10
});

This request will result in a timeout .

, $maxDistance, ( $maxDistance ). a $maxDistance , 1 . , , , - .

:

, LESS, . 8 . 10 , .

:

// Does not work. Ends up in an timeout
Locations.find({
  category: 'ABC', // has 9 locations
  geometry: { 
    '$near': { 
      $geometry: {
        type: "Point" ,
        coordinates: [ 13.357315063476564, 52.53167855932515 ]
      }
    } 
  } 
}, {
  limit: 10
});

// Works fine because of the $maxDistance - but in our case a $maxDistance is very BAD and needed to be prevented!
Locations.find({
  category: 'ABC', // has 9 locations
  geometry: { 
    '$near': { 
      $geometry: {
        type: "Point" ,
        coordinates: [ 13.357315063476564, 52.53167855932515 ]
      }
    },
    '$maxDistance': 5000 // If this value is too high - like the maxDistance is "the whole world" the query would also end up in an timeout 
  } 
}, {
  limit: 10
});

// Works fine because >= 10 items
Locations.find({
  category: 'DEF', // has 10 locations
  geometry: { 
    '$near': { 
      $geometry: {
        type: "Point" ,
        coordinates: [ 13.357315063476564, 52.53167855932515 ]
      }
    } 
  } 
}, {
  limit: 10
});

MongoDB 3.2.9

, MongoDB nodeJS. RoboMongo, : -!

+4
1

, 2 , category geometry. , , .

50 $maxDistance. , $maxDistance Mongo " " . .explain() , , IXSCAN ( BasicCursor Mongo) . , , , .

, ( ), , . , . , :

db.collection({category: 1, geometry: '2dsphere'});

, , 50 .

+1

Source: https://habr.com/ru/post/1661944/


All Articles