Closed6

Prisma + MongoDBで$nearをfindRawから呼び出すと "unable to find index for $geoNear query"

Sarutabiko | さるたびこ | 猿田彦Sarutabiko | さるたびこ | 猿田彦

下記のエラー

{ code: "unknown", message: "Command failed (NoQueryExecutionPlans): error processing query: ns=sarutabiko.XXXTree: GEONEAR field=loc maxdist=9310.19 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error :: caused by :: unable to find index for $geoNear query)" } })

下記のgeoIntersectsはこれまで動いていたので、nearが動かない理由が不明.

query = {
      filter: {
        loc: {
          $geoIntersects: {
            $geometry: {
              type: "Polygon",
              coordinates: [coordinates],
            },
          },
        },
      },
    };

$nearに投げたクエリは下記

query = {
      filter: {
        loc: {
          $nearSphere: {
            $geometry: {
              type: "Point",
              coordinates: centerCoordinates,
            },
          },
        },
      },
    };
Sarutabiko | さるたびこ | 猿田彦Sarutabiko | さるたびこ | 猿田彦

以前CreateIndexできているはずだし、geoIntersectsもgeoWithinもできるし問題ないはずと思って確認.

mongosh

で立ち上げて、

use データベース名
db.XXX.getIndexes()

を実行すると、

[ { v: 2, key: { _id: 1 }, name: '_id_' } ]

ない...

Sarutabiko | さるたびこ | 猿田彦Sarutabiko | さるたびこ | 猿田彦

db pushのたびにcreateIndexをするのも面倒なので、npm run buildに組み込む.

db = connect("mongodb://127.0.0.1:27017/DATABASE_NAME");
db.XXX.createIndex({ loc: "2dsphere" });

上記をsetup.jsとして、

mongosh -file setup.js

をpackage.jsonのbuildに追記.

このスクラップは2023/10/09にクローズされました