Nodejs mongodb geom 创建索引失败
Nodejs mongodb geom 创建索引失败
我有一张城市表,表的一条记录如下 { “_id” : ObjectId(“53fd8551cbbcd42488d50a5c”), “CODE” : 1003.0, “XCODE” : 350123.0, “SNAME” : “XX市”, “geom” : { “type” : “Polygon”, “coordinates” : [[[119.73287466488176, 26.440590523499242], [119.73237364734781, 26.441790564403114], [119.73263066139998, 26.442922591682265], [119.73313567644884, 26.443598615160887], [119.73363669240553, 26.443597614479636], [119.73438870944558, 26.443592610144869], [119.73538473647521, 26.442680586017158], [119.73537973601451, 26.441561553344538], [119.73437370463746, 26.440886533259395], [119.73287466488176, 26.440590523499242]]] }, “ELEV” : 0.0, “XNAME” : “YY县”, “ID” : 6.0 }
创建空间索引 db.native.ensureIndex({‘geom.coordinates’:‘2d’}) “errmsg” : "geo values must be ‘legacy coordinate pairs’ for 2d indexes Mongodb版本2.6.4。特来求助,为什么创建索引失败呢?
根据你的描述,你在使用 Node.js 和 MongoDB 创建一个包含地理空间数据的索引时遇到了问题。错误信息指出:“geo values must be ‘legacy coordinate pairs’ for 2d indexes”。这是因为你尝试为 geom
字段中的 coordinates
创建一个 2d 索引,但 2d 索引只支持简单的坐标对(即 [longitude, latitude]
),而你的 coordinates
是一个多边形的坐标数组。
解决方案
对于多边形或更复杂的几何图形,你应该使用 2dsphere 索引类型。以下是如何在 Node.js 中使用 Mongoose(一个流行的 MongoDB 对象模型工具)来创建 2dsphere 索引的示例代码:
const mongoose = require('mongoose');
// 定义 Schema
const citySchema = new mongoose.Schema({
CODE: Number,
XCODE: Number,
SNAME: String,
geom: {
type: {
type: String,
enum: ['Point', 'LineString', 'Polygon'],
required: true
},
coordinates: {
type: [Number],
required: true
}
},
ELEV: Number,
XNAME: String,
ID: Number
});
// 添加 2dsphere 索引
citySchema.index({ geom: '2dsphere' });
// 创建模型
const City = mongoose.model('City', citySchema);
// 连接到 MongoDB
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Failed to connect to MongoDB', err));
// 示例:插入一条数据
const city = new City({
CODE: 1003,
XCODE: 350123,
SNAME: "XX市",
geom: {
type: "Polygon",
coordinates: [
[[119.73287466488176, 26.440590523499242], [119.73237364734781, 26.441790564403114],
[119.73263066139998, 26.442922591682265], [119.73313567644884, 26.443598615160887],
[119.73363669240553, 26.443597614479636], [119.73438870944558, 26.443592610144869],
[119.73538473647521, 26.442680586017158], [119.73537973601451, 26.441561553344538],
[119.73437370463746, 26.440886533259395], [119.73287466488176, 26.440590523499242]]
]
},
ELEV: 0,
XNAME: "YY县",
ID: 6
});
city.save()
.then(savedCity => console.log('City saved:', savedCity))
.catch(err => console.error('Failed to save city:', err));
在这个示例中,我们定义了一个 citySchema
,并使用 2dsphere
索引来处理 geom
字段。这样可以正确地存储和查询多边形等复杂几何图形的数据。确保你的 MongoDB 版本支持 2dsphere 索引(MongoDB 2.6 及以上版本)。
根据你的描述,你在使用 MongoDB 创建一个空间索引时遇到了错误,提示 geo values must be 'legacy coordinate pairs' for 2d indexes
。这是因为 2d
索引只支持简单的二维坐标对(即 [x, y]
),而不支持嵌套数组的格式。
解决方案
你需要将存储的空间数据类型从 Polygon
改为简单坐标对(例如 [longitude, latitude]
)。如果你希望使用复杂的空间数据类型,建议使用 2dsphere
索引类型。
使用 2dsphere
索引
- 修改文档结构:确保你的
geom
字段是符合2dsphere
索引要求的格式。 - 创建索引:使用
2dsphere
索引来创建索引。
示例代码
const mongoose = require('mongoose');
// 定义 Schema
const citySchema = new mongoose.Schema({
CODE: Number,
XCODE: Number,
SNAME: String,
geom: {
type: { type: String, default: "Point" },
coordinates: [Number]
},
ELEV: Number,
XNAME: String,
ID: Number
});
// 连接到 MongoDB
mongoose.connect('mongodb://localhost/cities', { useNewUrlParser: true, useUnifiedTopology: true });
// 创建模型
const City = mongoose.model('City', citySchema);
// 创建一个示例数据
const city = new City({
CODE: 1003,
XCODE: 350123,
SNAME: "XX市",
geom: {
type: "Polygon",
coordinates: [
[
[119.73287466488176, 26.440590523499242],
[119.73237364734781, 26.441790564403114],
[119.73263066139998, 26.442922591682265],
[119.73313567644884, 26.443598615160887],
[119.73363669240553, 26.443597614479636],
[119.73438870944558, 26.443592610144869],
[119.73538473647521, 26.442680586017158],
[119.73537973601451, 26.441561553344538],
[119.73437370463746, 26.440886533259395],
[119.73287466488176, 26.440590523499242]
]
]
},
ELEV: 0,
XNAME: "YY县",
ID: 6
});
// 保存示例数据
city.save((err) => {
if (err) {
console.error(err);
} else {
console.log("Data saved successfully");
}
});
// 创建 2dsphere 索引
citySchema.index({ geom: "2dsphere" });
这段代码定义了一个城市模型,并创建了一个 2dsphere
索引。确保你的 geom
字段是符合 2dsphere
索引要求的格式。