node.js - mongodb $text 与 $near

标签 node.js mongodb geospatial

好的,我的目标是对我的收藏执行文本搜索,然后过滤这些结果以确保它们在我的“ donut ”几何范围内。来自 mongo 站点的示例:

enter image description here

这是困难的部分。 Mongo 的文档证实,今天您无法将 $text 和 $near 的优点结合起来:

You cannot combine the $near operator, which requires a special geospatial index, with a query operator or command that uses a different type of special index. For example you cannot combine $near with the $text query.

Sooo.. 这就是我今天要做的事情。请注意,我当前的方法没有实现“ donut ”几何形状(它只是一个直径不断增大的圆,当用户在 map 上“缩小”时会返回重复的结果)。

    var vendorResponse = JSON.parse(body);
    var vendorPosts = vendorResponse.postings;

    //Get the location of the user
    var userLat = req.query.lat;
    var userLong = req.query.long;

    //Get the item that furthest away from the user
    var furthestitem = sortedVendorPosts[sortedVendorPosts.length - 1];

    //Calculate the radius of the area
    var radius = Math.sqrt( Math.pow((userLat-furthestitem.location.lat), 2) + Math.pow((userLong-furthestitem.location.long), 2) )

    PostModel.find({
        $text: { $search: heading, $language: 'english' },
        $or: mongoCategories,
        coordinates :
             { $geoWithin :
                 {
                     $centerSphere : [ [ userLong, userLat ] , radius ]
                 }
             }
        } , { score: {
                $meta: "textScore"
            }
        }, function (err, results) {




        });

我曾尝试使用 mongos $geoIntersects 方法,但我正在用头撞墙来制定此查询。如何解决 mongos 电流限制的详细示例将是天赐之物!谢谢大家!

最佳答案

在我的案例场景中似乎有几个解决方案:

  1. 使用 Mongo Connect 并集成诸如 solr 或 elasticsearch 之类的东西
  2. 使用 Mongo 的 $in 方法执行“嵌套查询”.. 这需要对数据库进行两次查询
  3. 使用 Mongo 的 $regex 方法(正如其他人所描述的,我在下面进行了演示)。

    PostModel.find({
        coordinates: { $near: {
            $geometry: { type: "Point", coordinates: [ userLong , userLat ] },
            $maxDistance: maxDistance,
            $minDistance: minDistance
            }
        },
        $or: mongoCategories,
        term: { "$regex": term, "$options": 'i' }
    }, function (err, results) {
        //systematic error. Redirect user so they can report error.
        if (err) {
            console.log(err);
            callback(body);
        // if no result found
        } else if (!results) {  
            callback(results);
        } else if (results) {
            callback(results);
        }
    });
    

我已经联系了 Mongodb 团队,了解我们何时能够跨多个索引执行搜索。希望这对您有所帮助!

关于node.js - mongodb $text 与 $near,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25922965/

相关文章:

node.js - 如何确定包含多个流的流中文件的结尾? ( Node )

mongodb - 为什么 ObjectId 使 MongoDB 中的分片更容易?

mysql - 经纬度 MySQL 字段类型

sql-server - 在 SQL Server 2008 中使用 STIntersects 的问题

geolocation - 当使用geo_distance过滤器时,ElasticSearch返回的项目太远

css - 在使用 Node 进行 SASS 编译期间注入(inject)变量

node.js - 找不到模块; Hyperledger Fabric 教程

javascript - 使用 NodeJS 和 Mongoose 在 MongoDB 中打开多个连接

node.js - 从 MJPEG 流中提取 JPEG 并通过 websocket 发布 base64 编码图像

node.js - 按 id 删除记录?