mongodb 基于地理位置的查询
2016-01-08 15:33
651 查看
需求:
查询结果按照到某一点的距离排序。schema定义:
var UserSchema = new Schema({ ... location:[Number] //经纬度数组 }); UserSchema.index({'location': '2dsphere'});
查询语句
User.find({ location: { $nearSphere: { $geometry: { type : "Point", coordinates : [116.411190, 40.005540] } } } });
查询出来的结果是 user 表 location 距离坐标[116.411190, 40.005540]由近到远排序的,不能同时使用其他排序字段。
参考:
http://stackoverflow.com/questions/24297556/mongoose-aggregation-with-geonear
https://docs.mongodb.org/manual/reference/operator/query/nearSphere/
http://tech.meituan.com/lucene-distance.html
相关文章推荐
- Mongodb使用时用_id作为检索条件的一个问题
- MongoDB基本命令
- MongoDB 语法使用小结
- windows环境下Mongodb分片配置
- Python写mongodb mapreduce实例
- mongodb时间戳转换成格式化时间戳
- MongoDB安装
- MongoVE连接MongoDB 不显示数据问题
- mongoDB 3.0 安全权限访问控制
- MongoDb gridfs-ngnix文件存储方案
- MongoDB 数据类型
- ExpressJS File Uploading – GridFS – MongoDB
- mongodb的基本配置
- MongoDB在Linux下安装和启动和配置
- Linux下MongoDB的安装、配置、启动
- window平台安装 MongoDB
- mongoose封装管理mongodb的公共模块
- MongoDB数据逻辑结构
- MongoDB在Windows 平台的安装
- 初识 MongoDB