gpt4 book ai didi

javascript - Mongoose 无法识别我的 2dsphere 索引

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

我正在尝试为 tourSchema 中存在的字段 startLocation 添加 2dSphere 索引。下面是它的样子

   startLocation: {
type: {
type: String,
default: 'Point',
enum: ['Point']
},
coordinates: [Number],
address: String,
description: String
}

您还可以在下面看到我在此架构上添加了哪些索引以及如何添加索引

tourSchema.index({price:1,ratingsAverage:-1});
tourSchema.index({slug:1});
tourSchema.index({ startLocation: '2dsphere' });

不幸的是Mongodb无法识别startLocation索引。使用 Mongo Compass ,我可以查看我创建的所有索引(startLocation:'2dsphere' 除外)。

这是当我向 Controller 中的 getDistances 方法发送请求时 postman 给我的错误:

{
"status": "error",
"error": {
"operationTime": "6791492473605586945",
"ok": 0,
"errmsg": "$geoNear requires a 2d or 2dsphere index, but none were found",
"code": 27,
"codeName": "IndexNotFound",
"$clusterTime": {
"clusterTime": "6791492473605586945",
"signature": {
"hash": "4LYCSBslSoLAoqj93bLXmpubBxs=",
"keyId": "6779443539857113090"
}
},
"name": "MongoError",
"statusCode": 500,
"status": "error"
},
"message": "$geoNear requires a 2d or 2dsphere index, but none were found",
"stack": "MongoError: $geoNear requires a 2d or 2dsphere index, but none were found\n at Connection.<anonymous> (C:\\Users\\timuc\\Downloads\\starter\\starter\\node_modules\\mongodb-core\\lib\\connection\\pool.js:443:61)\n at Connection.emit (events.js:223:5)\n at processMessage (C:\\Users\\timuc\\Downloads\\starter\\starter\\node_modules\\mongodb-core\\lib\\connection\\connection.js:364:10)\n at TLSSocket.<anonymous> (C:\\Users\\timuc\\Downloads\\starter\\starter\\node_modules\\mongodb-core\\lib\\connection\\connection.js:533:15)\n at TLSSocket.emit (events.js:223:5)\n at addChunk (_stream_readable.js:309:12)\n at readableAddChunk (_stream_readable.js:290:11)\n at TLSSocket.Readable.push (_stream_readable.js:224:10)\n at TLSWrap.onStreamRead (internal/stream_base_commons.js:181:23)"
}

我尝试添加 mongodb 识别的 point: '2dsphere' 但我不满意。因为当我向 Controller 中的方法发送请求时,该方法返回成功,但数组为空。

这是在 Controller 中触发的方法:

exports.getDistances = catchAsync(async (req, res, next) => {
const { latlng, unit } = req.params;
const [lat, lng] = latlng.split(",");
if (!lat || !lng) {
new AppError( "Please provide latitude and longitude in the format lat,lng", 400);
}

const distances = await Tour.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: [lng * 1, lat * 1]
},
distanceField: "distance"
}
}
]);

res.status(200).json({
status: "success",
data: {
data: distances
}
});
});

您还可以从路由器中看到我如何发送下面的请求 URL

tourRouter.route('/distances/:latlng/unit/:unit').get(tourController.getDistances);

最佳答案

我坚信您没有使用正确的集合。这适用于 MongoDB 4.2。

创建索引:

db.location.createIndex({
startLocation: "2dsphere"
})

该集合的索引:

db.location.getIndexes()
[{
"v": 2,
"key": {
"_id": 1
},
"name": "_id_",
"ns": "stackoverflow.location"
}, {
"v": 2,
"key": {
"startLocation": "2dsphere"
},
"name": "startLocation_2dsphere",
"ns": "stackoverflow.location",
"2dsphereIndexVersion": 3
}
]

插入一些数据:

db.location.insert({
startLocation: {
type: "Point",
coordinates: [40, 5],
address: "Hellostreet 1",
description: "Hello"
}
})

聚合集合:

db.location.aggregate([{
$geoNear: {
near: {
type: 'Point',
coordinates: [41, 6]
},
distanceField: 'distance'
}
}
])

结果:

{
"_id" : ObjectId("5e404cdd13552bde0a0a9dc5"),
"startLocation" : {
"type" : "Point",
"coordinates" : [
40,
5
],
"address" : "Hellostreet 1",
"description" : "Hello"
},
"distance" : 157065.62445348964
}

关于javascript - Mongoose 无法识别我的 2dsphere 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60139209/

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