- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在这方面遇到了很大的麻烦。我正在构建一个小 API 来获取给定特定坐标的最接近的存储元素(在 mlab MongoDB 中)。
我想使用 Mongo 自定义查询工具(例如 $near)从数据库中获取最接近的元素。
您可以找到原始存储库 here !
原始数据是一个小的.csv,我将其转换为geojson格式。
这是该 .csv 的简短版本:
这是它的 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"
}
}
]
}
<小时/>
这是使用 mongoose 的架构(如您所见,我确实添加了 2dsphere 索引):
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;
<小时/>
现在这是我正在努力进行的测试(使用 Mocha):
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 索引的方式。但我仍然不明白为什么它说找不到索引。即使我用 waitForIndex() 或 MapEvent.init() 等待它。
这个社区创造了一些很酷的奇迹,我希望你能帮助我。
谢谢
最佳答案
此类错误表明索引未创建。通常这是由于坐标数据无效造成的。也许加载您的 mongo shell 并确保您的索引存在:
mongo
use yourdatabasename
db.yourcollection.getIndexes()
你最终应该得到类似的结果:
{
"v" : 2,
"key" : {
"geometry" : "2dsphere"
},
"name" : "geometry_2dsphere",
"ns" : "databasename.yourcollection",
"2dsphereIndexVersion" : 3
}
如果不存在,请尝试在 shell 中创建索引 - 如果无法创建索引,则会抛出错误。:
db.yourcollection.createIndex( { geometry : "2dsphere" } )
关于node.js - $接近错误 - 规划器返回错误 : unable to find index for $geoNear query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276519/
这个问题在这里已经有了答案: android studio adb Syntax error: ")" unexpected (4 个答案) 关闭 5 年前。 我确实将我的工作室更新到 Linux
当我打开 Android Studio 时,它会显示下面的对话框,我的应用程序无法以 Debug模式运行。 最佳答案 找到sdk->platform-tool文件夹,然后运行命令adb tcpip 5
我在 Fedora 33 机器上使用了 VSCode (1.52.1) 一段时间。 我正在使用 Docker 扩展 (v1.9.0) 但由于某种原因,在过去几天中,当我尝试将 VSCode 附加到 D
我正在尝试为我的任务加载一个名为“tr_model.h5”的预训练模型,但出现以下错误: Traceback (most recent call last): File "Trigger_Proje
我最近在 Google Play 中发布了一个 Android 应用程序,到目前为止一切看起来都很棒,但有一个异常(exception)(见下文),我有时会收到控制台崩溃的消息,它只发生在某些用户身上
单击警告时,我收到此消息。这是案件的截图。 错误写道, Unable to open 'warning.cpp': Unable to read file '/Users/dimen/code/C++
重新编译 php 后,当我使用 php cli 时出现以下错误: PHP Warning: PHP Startup: imap: Unable to initialize module Module
使用 git 1.6.4.2,当我尝试 git pull 时出现此错误: error: unable to resolve reference refs/remotes/origin/LT558-op
我是 Kotlin 的新手,我正在学习教程。运行我的应用程序会导致它在运行时崩溃。我在底部导航栏应用程序中有三个 fragment 和一个主要 Activity 。我的目标实际上只是成功运行该应用程序
我在 Windows 10 64 位上运行 Android Studio。我在 5 月 20 日早上升级到了最新的版本和 SDK,从那时起,我在打开 Android Studio 时收到上述错误。我还
因此,我创建了一个 GitHub 操作,该操作应该在发生推送时构建 docker 镜像并将其推送到 docker hub。所以这是我的 GitHub 操作:(第一次创建 GitHub 操作) name
当这些问题中的任何一个都没有帮助我时,这意味着我需要删除并重新安装 Android Studio。 这是错误: Error initializing ADB: Unable to create Deb
Iam running the latest OSX/Flutter/XCode Versions using flutter, android studio and firebase and
我最近重新安装了 ubuntu 20.04.3,在做了一些设置后,我现在在运行 apt update 时总是收到以下错误.我一定是不小心删除了某种缓存文件或目录,但我不知道如何诊断或解决这个问题。 任
今天,当我将更改推送到 Gitlab 中的代码存储库时,我才开始在 Gitlab 中收到此错误消息。 Gitlab fatal: unable to access '.......' SSL Cert
我正在使用 Protractor 和 Jasmine 。 我已经确定了 chrome 驱动程序版本:2.32.498550 (latest)与 Chrome 测试版不兼容 (Version 62.0.
我正在尝试使用 terraform (0.12.24) 和多个 Docker 提供程序(插件版本 2.7.0)执行简单部署。我使用下面的 Terraform 模板的目的是将两个不同的容器部署到两个不同
这个问题我看了很久,最后决定在这里提出来。我需要维护一些用 Delphi 2007 for .NET (ASP.NET 2.0) 编写的应用程序。通常,第一次运行应用程序(使用 IIS)时,我会收到经
如何修复 - 无法创建调试桥:无法启动 adb 服务器:无法检测 adb 版本,adb 输出:/home/dilip/Downloads/sdk/platform-tools/adb: 1:/home
通过命令提示符连接到 android 中的 Sqlite DB 时出现错误。 以下是我遵循的步骤: 我已经在 android 中通过 java 程序创建了 Sqlite 数据库。创建表并向其中插入数据
我是一名优秀的程序员,十分优秀!