gpt4 book ai didi

node.js - Mongoose 根据经纬度信息和文本索引对文档进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:20 24 4
gpt4 key购买 nike

我正在开发搜索功能,用户可以在其中搜索某些餐厅。场景是:

我正在收集餐厅,其中有多个位置及其纬度和经度信息。现在在搜索过程中,

我有用户的位置信息(带有纬度和经度)。因此,我必须根据用户位置从最近到最远对餐厅进行排序,并且必须对使用文本索引索引的字段的内容进行文本搜索。

我有不同的搜索场景,因此我使用聚合管道和投影来对结果进行评分。有人可以建议我从哪里开始吗?

提前致谢。

最佳答案

尝试以下步骤:

第一步:

在架构中添加位置字段并在其上创建 2dsphere 索引。

location: { type: [Number], index: '2dsphere' }

您还可以动态创建索引:

db.collection.createIndex( { <location field> : "2dsphere" } )

步骤2

现在使用 $geoNear 创建聚合查询:

var aggregateQuery = [{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [options.longitude, options.latitude]
},
"distanceField": 'dis',
"distanceMultiplier": .001,
"spherical": true,
"query": **conditions**
}
}];

您可以在查询参数中传递任何条件。

db.collectionname.aggregate(aggregateQuery).exec(function(err, result){
// use result here
})

注意:请记住首先将经度存储在位置数组中,然后存储纬度。

更多详情请引用此链接: https://docs.mongodb.com/manual/reference/command/geoNear/

希望这有帮助!!

更新:

db.collectionname.aggregate([
{$match: {
$text: {$search: "text"} ,
location: {$geoWithin: {$centerSphere: [[ longitude, latitude], points]}}
}}])

注意:不会使用地理索引!

有关更多信息,请参阅此链接 http://docs.mongodb.org/manual/reference/operator/query/geoWithin/

关于node.js - Mongoose 根据经纬度信息和文本索引对文档进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39787532/

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