$ near error - планировщик вернул ошибку: не удалось найти индекс для запроса $ geoNear - PullRequest
0 голосов
/ 11 сентября 2018

У меня большие проблемы с этим.Я создаю небольшой API для получения ближайших хранимых элементов (в mlab MongoDB) с заданными конкретными координатами.

Я хочу получить самые близкие элементы из моей БД, используя инструменты пользовательского запроса Mongo, такие как $ near.

Вы можете найти оригинальное репо здесь !

Данные

Исходные данные - это небольшой файл .csv, который я преобразовал в формат геоджон.

Вот краткая версия этого файла .csv:

A little csv containing events coordinates

Вот его версия Geojson:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.4049238868200975, 48.82094216189432]
      },
      "properties": {
        "event_type": "imp"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.3645320381923534, 48.816341825787724]
      },
      "properties": {
        "event_type": "imp"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.3274838513968072, 48.86982967859056]
      },
      "properties": {
        "event_type": "imp"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.23284629237154, 48.89111120713999]
      },
      "properties": {
        "event_type": "imp"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.23284629237154, 48.89111120713999]
      },
      "properties": {
        "event_type": "click"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.4204737962258305, 48.85038737901075]
      },
      "properties": {
        "event_type": "imp"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.4214870126092594, 48.86339618816508]
      },
      "properties": {
        "event_type": "imp"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.4144620076022436, 48.876530301936576]
      },
      "properties": {
        "event_type": "imp"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [2.2470618097659285, 48.88845096504584]
      },
      "properties": {
        "event_type": "imp"
      }
    }
  ]
}

Модель

Вот схема с использованием мангусты(как вы можете видеть, я добавил 2dsphere index ):

const mongoose = require("mongoose");

const FeatureSchema = new mongoose.Schema({
  type: String,
  geometry: {
    type: { type: String, default: "Point" },
    coordinates: { type: [Number] }
  },
  properties: { event_type: String }
});

FeatureSchema.index({ geometry: "2dsphere" });
const MapEventSchema = new mongoose.Schema({
  type: String,
  features: [FeatureSchema]
});

const MapEvent = mongoose.model("mapEvent", MapEventSchema);

module.exports = MapEvent;

The Test

Вот теперь тест (с Мокко), который яя борюсь с:

const assert = require("assert");
const MapEvent = require("../src/models/mapEvents");
const fs = require("fs");

describe("Read test", () => {
  // create new room collection because collection is drop between each file
  beforeEach(done => {
    rawJSON = fs.readFileSync("./src/assets/test_assets/read_test.json");
    const parsedContent = JSON.parse(rawJSON);
    const mapEvent = new MapEvent(parsedContent);
    mapEvent
      .save()
      .then(() => {
        done();
      })
      .catch(error => {
        console.error(error);
      });
  });

  it("Find nearest event by coordonates", done => {
    const distance = 1000;
    const lat = 48.86;
    const lon = 2.35;

    function waitForIndex() {
      return new Promise((resolve, reject) => {
        MapEvent.on("index", error => (error ? reject(error) : resolve()));
      });
    }

    MapEvent.findOne({
      geometry: {
        $near: [lat, lon],
        $maxDistance: distance
      }
    })
      //.then(waitForIndex)
      .then(MapEvent.init())
      .then(nearestResult => {
        console.log("------------");
        console.log(nearestResult);
        console.log("------E-----");
        assert(nearestResult);
        done();
      })
      .catch(error => {
        console.error("************");
        console.error(error);
        console.error("******E*****");
      });
  });
}); 

Выходные данные

Как вы можете заметить, я закомментировал waitForIndex () (найти по связанному вопросу Stackoverflow), чтобы использовать MapEvent.init ()вместо этого (найти на github).К сожалению, оба решения выдают одинаковую ошибку.

Connection is established
  Create test
(node:12483) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
    ✓ MapEvent saving

  Read test
************
{ MongoError: error processing query: ns=babylon_ad_db.mapevents batchSize=1 limit=1Tree: GEONEAR  field=geometry maxdist=1000 isNearSphere=0
Sort: {}
Proj: {}
 planner returned error: unable to find index for $geoNear query
    at queryCallback (/home/geoffrey/babylon-ad/node_modules/mongodb-core/lib/cursor.js:248:25)
    at /home/geoffrey/babylon-ad/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at process._tickCallback (internal/process/next_tick.js:61:11)
  ok: 0,
  errmsg:
   'error processing query: ns=babylon_ad_db.mapevents batchSize=1 limit=1Tree: GEONEAR  field=geometry maxdist=1000 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
  code: 2,
  codeName: 'BadValue',
  operationTime:
   Timestamp { _bsontype: 'Timestamp', low_: 7, high_: 1536667477 },
  '$clusterTime':
   { clusterTime:
      Timestamp { _bsontype: 'Timestamp', low_: 7, high_: 1536667477 },
     signature: { hash: [Binary], keyId: [Long] } },
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} }
******E*****
    1) Find nearest event by coordonates


  1 passing (3s)
  1 failing

  1) Read test
       Find nearest event by coordonates:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/geoffrey/babylon-ad/test/read_test.js)

Я уже немного погуглил, вот как я слышал о $ near и 2dsphere index.Но все же я не могу понять, почему он говорит, что не может найти индекс.Даже когда я жду его с помощью waitForIndex () или MapEvent.init ().

Это сообщество совершило несколько чудесных чудес, надеюсь, вы мне поможете.

Спасибо,

1 Ответ

0 голосов
/ 19 сентября 2018

Этот тип ошибки указывает, что индекс не был создан.Обычно это происходит из-за неверных координатных данных.Возможно, загрузите вашу оболочку mongo и убедитесь, что ваши индексы присутствуют:

mongo
use yourdatabasename
db.yourcollection.getIndexes()

В результате вы должны получить что-то вроде:

{
    "v" : 2,
    "key" : {
      "geometry" : "2dsphere"
    },
    "name" : "geometry_2dsphere",
    "ns" : "databasename.yourcollection",
    "2dsphereIndexVersion" : 3
  }

, если его нет, попробуйте создать индекс в оболочке- он выдаст ошибки, если не сможет их создать .:

db.yourcollection.createIndex( { geometry : "2dsphere" } )
...