Mongodb 平均球面半径搜索
我对stack(以及一般的编程)是个新手,但我有一个简单的应用程序 我的模型如下所示:Mongodb 平均球面半径搜索,mongodb,mongoose,mean-stack,2dsphere,Mongodb,Mongoose,Mean Stack,2dsphere,我对stack(以及一般的编程)是个新手,但我有一个简单的应用程序 我的模型如下所示: var mongoose = require('bluebird').promisifyAll(require('mongoose')); var RestaurantSchema = new mongoose.Schema({ name: String, street: String, zip: Boolean, city: String, country: String, loc
var mongoose = require('bluebird').promisifyAll(require('mongoose'));
var RestaurantSchema = new mongoose.Schema({
name: String,
street: String,
zip: Boolean,
city: String,
country: String,
location: {
coordinates: {type: [Number], index: '2dsphere'}
},
kitchen: String,
rating: Number
});
RestaurantSchema.index({location: '2dsphere'});
export default mongoose.model('Restaurant', RestaurantSchema);
我在数据库中添加以下内容:
Restaurant.find({}).removeAsync()
.then(() => {
Restaurant.create({
name: 'Kaspers restaurant',
street: 'Nørrebrogade 1',
zip: '2200',
city: 'Copenhagen',
country: 'Denmark',
location: ['12.548057','55.7034'],
kitchen: 'Italian',
rating: '4.5'
}, {
name: 'Kristinas restaurant',
street: 'Østerbrogade 2',
zip: '2200',
city: 'Copenhagen',
country: 'Denmark',
location: ['12.541565', '55.689206'],
kitchen: 'Thai',
rating: '5'
}, {
name: 'Noma',
street: 'Strandgade 93',
zip: '1401',
city: 'Copenhagen',
country: 'Denmark',
location: ['12.59620', '55.677933'],
kitchen: 'Nordisk',
rating: '6'
});
});
My controller通过以下方式提取用户位置并列出餐厅:
angular.module('venueApp')
.controller('RestaurantsCtrl', function ($scope, $http, $window) {
$http.get('/api/restaurants')
.success(function(data) {
$scope.restaurants = data;
console.log($scope.restaurants);
})
.error(function(err) {
console.log('Error! Something went wrong');
});
$window.navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
$scope.$apply(function () {
$scope.lat = lat;
$scope.lng = lng;
});
});
});
<navbar></navbar>
<div>
This is your location:
<input type="text" class="disabled" placeholder="{{lat}}">
<input type="text" class="disabled" placeholder="{{lng}}">
<br>
How far do you want to search:
<div class="form-group">
<label for="distance">Max. Distance (miles)</label>
<input type="text" class="form-control" id="distance" placeholder="500" ng-model="formData.distance">
</div>
</div>
<div class="col-md-12">
<table class="table table-striped">
<thead>
<th>Navn</th>
<th>By</th>
<th>Køkken</th>
<th>Latitude</th>
<th>Longtitude</th>
</thead>
<tbody>
<tr ng-repeat="restaurant in restaurants">
<td>{{restaurant.name}}</td>
<td>{{restaurant.city}}</td>
<td>{{restaurant.kitchen}}</td>
<td>{{restaurant.location.coordinates[0]}}</td>
<td>{{restaurant.location.coordinates[1]}}</td>
</tr>
</tbody>
</table>
“我的视图”通过以下方式显示用户位置、获取最大输入并列出餐厅:
angular.module('venueApp')
.controller('RestaurantsCtrl', function ($scope, $http, $window) {
$http.get('/api/restaurants')
.success(function(data) {
$scope.restaurants = data;
console.log($scope.restaurants);
})
.error(function(err) {
console.log('Error! Something went wrong');
});
$window.navigator.geolocation.getCurrentPosition(function(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
$scope.$apply(function () {
$scope.lat = lat;
$scope.lng = lng;
});
});
});
<navbar></navbar>
<div>
This is your location:
<input type="text" class="disabled" placeholder="{{lat}}">
<input type="text" class="disabled" placeholder="{{lng}}">
<br>
How far do you want to search:
<div class="form-group">
<label for="distance">Max. Distance (miles)</label>
<input type="text" class="form-control" id="distance" placeholder="500" ng-model="formData.distance">
</div>
</div>
<div class="col-md-12">
<table class="table table-striped">
<thead>
<th>Navn</th>
<th>By</th>
<th>Køkken</th>
<th>Latitude</th>
<th>Longtitude</th>
</thead>
<tbody>
<tr ng-repeat="restaurant in restaurants">
<td>{{restaurant.name}}</td>
<td>{{restaurant.city}}</td>
<td>{{restaurant.kitchen}}</td>
<td>{{restaurant.location.coordinates[0]}}</td>
<td>{{restaurant.location.coordinates[1]}}</td>
</tr>
</tbody>
</table>
这是您的位置:
您要搜索多远:
最大距离(英里)
纳文
通过
科肯
纬度
冗长
{{餐厅名称}
{{餐厅.城市}
{{餐厅.厨房}
{{餐厅.位置.坐标[0]}
{{餐厅.位置.坐标[1]}
我已经使用了约曼角全堆栈发生器。在我的html视图中,我提取用户当前的位置“lat”和“lng”,然后通过数据库搜索距离用户最大的餐厅
在这个脚手架上我该怎么做?
我已尝试让这些示例/教程发挥作用,但没有效果:(
“位置”:{“坐标”:[12.541565,55.689206]}
或将模式更改为“位置”:[数字]
。还要注意,您应该使用数值而不是字符串,即使mongoose将为您转换正确的类型。然后,这是一个简单的查询问题,或者在何处指定半径。您好。谢谢。将架构更改为“位置”:{“坐标”:[coord1,coord2]。您能帮我准确地向控制器添加什么吗?我的重点是您的“种子”数据的结构不正确。文档注释中的链接应该有足够的示例说明所需的查询运算符用法。