gpt4 book ai didi

node.js - 查询提示缺少 2dsphere-index,但它确实存在

转载 作者:太空宇宙 更新时间:2023-11-03 23:23:34 25 4
gpt4 key购买 nike

当我执行以下代码时(一个更大的示例,归结为要点)

var mongoose = require("mongoose");

var LocationSchema = new mongoose.Schema({
userName: String,
loc: {
'type': { type: String, enum: "Point", default: "Point" },
coordinates: { type: [Number] }
}
})
LocationSchema.index({ category: 1, loc: "2dsphere" });
var Location = mongoose.model("location", LocationSchema);
var mongoDB = 'mongodb://user1:test@ds042417.mlab.com:42417/locationdemo';
mongoose.Promise = global.Promise;
mongoose.connect(mongoDB, { useMongoClient: true });

var testUser = Location({
userName: "Tester",
loc: { coordinates: [12.44, 55.69] }
});
testUser.save(function (err) {
if (err) {
return console.log("UPPPPs: " + err);
}
console.log("User Saved, Try to find him:");

let query = Location.find({
loc:
{
$near:
{
$geometry:
{
type: "Point",
coordinates: [12.50, 55.71]
},
$maxDistance: 600000
}
}
})
query.exec(function (err, docs) {
if (err) {
return console.log("Err: " + err);
}
console.log("Found: " + JSON.stringify(docs))
})
});

我收到此错误:

Err: MongoError: error processing query: ns=locationdemo.locationsTree: GEONEAR field=loc maxdist=600000 isNearSphere=0 Sort: {} Proj: {} planner returned error: unable to find index for $geoNear query

但是索引在那里(参见第 10 行)和下面 mlab 的屏幕截图。我究竟做错了什么?: enter image description here

最佳答案

您违反了一般如何使用索引的规则。虽然确实有 no restriction “2dsphere”索引是复合索引中的“第一个”属性,但是,您的“查询”实际上要解决第一个属性以便选择索引,这一点非常重要。

这在 Prefixes 中有介绍。来自复合索引手册。摘录如下:

{ "item": 1, "location": 1, "stock": 1 }

The index has the following index prefixes:

  • { item: 1 }
  • { item: 1, location: 1 }

For a compound index, MongoDB can use the index to support queries on the index prefixes. As such, MongoDB can use the index for queries on the following fields:

  • the item field,
  • the item field and the location field,
  • the item field and the location field and the stock field.

However, MongoDB cannot use the index to support queries that include the following fields since without the item field, none of the listed fields correspond to a prefix index:

  • the location field,
  • the stock field, or
  • the location and stock fields.

因为您的查询引用"loc" 首先并且不包括"category" ,索引没有被选择,MongoDB 返回错误。

因此,为了使用您定义的索引,您需要实际查询 "category"以及。修改您的列表:

var mongoose = require("mongoose");

mongoose.set('debug',true);

var LocationSchema = new mongoose.Schema({
userName: String,
category: Number,
loc: {
'type': { type: String, enum: "Point", default: "Point" },
coordinates: { type: [Number] }
}
})
//LocationSchema.index({ loc: "2dsphere", category: 1 },{ "background": false });
LocationSchema.index({ category: 1, loc: "2dsphere" });

var Location = mongoose.model("location", LocationSchema);
var mongoDB = 'mongodb://localhost/test';
mongoose.Promise = global.Promise;
mongoose.connect(mongoDB, { useMongoClient: true });


var testUser = Location({
userName: "Tester",
category: 1,
loc: { coordinates: [12.44, 55.69] }
});

testUser.save(function (err) {
if (err) {
return console.log("UPPPPs: " + err);
}
console.log("User Saved, Try to find him:");

let query = Location.find({
category: 1,
loc:
{
$near:
{
$geometry:
{
type: "Point",
coordinates: [12.50, 55.71]
},
$maxDistance: 600000
}
}
})
query.exec(function (err, docs) {
if (err) {
return console.log("Err: " + err);
}
console.log("Found: " + JSON.stringify(docs))
})
});

只要我们包含 "category"一切都很好:

User Saved, Try to find him:
Mongoose: locations.find({ loc: { '$near': { '$geometry': { type: 'Point', coordinates: [ 12.5, 55.71 ] }, '$maxDistance': 600000 } }, category: 1 }, { fields: {} })
Found: [{"_id":"59f8f87554900a4e555d4e22","userName":"Tester","category":1,"__v":0,"loc":{"coordinates":[12.44,55.69],"type":"Point"}},{"_id":"59f8fabf50fcf54fc3dd01f6","userName":"Tester","category":1,"__v":0,"loc":{"coordinates":[12.44,55.69],"type":"Point"}}]

另一种情况是简单地为索引添加位置“前缀”。确保首先删除以前的索引或集合:

LocationSchema.index({ loc: "2dsphere", category: 1 },{ "background": false });

您可能应该养成设置 "background": true 的习惯,否则您会开始在单元测试中遇到竞争条件,其中在单元测试代码尝试使用索引之前索引尚未完成创建。

关于node.js - 查询提示缺少 2dsphere-index,但它确实存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47037053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com