【问题标题】:mongodb mongoose query for a point where coordinates=[longitude,latitude]mongodb mongoose 查询坐标=[longitude,latitude] 的点
【发布时间】:2016-03-06 17:12:41
【问题描述】:

我已经成功创建了一个存储经度、纬度类型点的模式。我已经使用 2dsphere 对其进行了索引。我正在使用 mongodb 和猫鼬。

这是我的架构的代码 sn-p

var locationSchema = new mongoose.Schema({
        loc : { type: {type:String}, 坐标: [Number]},
        位置名称:字符串,

})

locationSchema.index({loc: '2dsphere'});

我可以在表格中成功插入位置数据。

我想使用 mongoose 和在 mongodb 中执行类似于“从 DB 中选择坐标 = [经度,纬度]”的操作。

使用 mongodb ,我尝试使用 db.collections.find({loc:{type:"point",coordinates:[longitude,latitude]}) 和同一查询的其他几种语法变体,但没有成功.

有人可以指出正确的方向吗?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    你试过回调吗?

    db.collections.find({loc:{type:"point"}}, function(err, data){
      if(err) throw err;
      console.log(data)
      // Do a for loop where data.cordinates === longitude,latitude..
    })
    

    【讨论】:

    • 当我使用上面的代码时,我添加了一个带有 console.log 语句的条件 if(data.length==0) ,并且总是在 console.log 里面如果条件被执行
    • 即使数据不为空?你检查data.length 没有输出 0 吗?
    • 在 mongoDB 控制台中,当您要搜索具有给定纬度和经度的文档时,以下查询有效 db.collection.find({loc.coordinates: [lon,lat]} , {_id:1}, 回调)
    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2021-10-14
    • 2014-10-10
    • 2018-04-02
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多