我想通过应用以下过滤器进行 mongodb 查询来搜索公司。
{
myLocation: [lng1, lat1], // That is to get distance from current location to companies on result.
region: [lng2, lat2], //That is to get companies within a radius in this region
radius: 10000,
tags: ["tag1", "tag2"],
sort: "asc" // to sort by nearest from current location.
}
Run Code Online (Sandbox Code Playgroud)
我认为它需要使用 $geoNear 两次并进行这样的聚合。
{
$geoNear: {
near: {
type: "Point",
coordinates: myLocation
},
distanceField: 'distance1',
maxDistance: radius,
spherical: true,
},
$match: { tags: tags },
$geoNear: {
near: {
type: …
Run Code Online (Sandbox Code Playgroud) 这似乎是一个常见的错误,但我似乎无法使其与我见过的所有建议一起工作。这是我的设置:
// point.js (based on mongoose recommended subdocument pattern for reusing the GeoJSON definition
// see here https://mongoosejs.com/docs/geojson.html)
const mongoose = require('mongoose');
const pointSchema = new mongoose.Schema({
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true,
}
});
exports = pointSchema;
Run Code Online (Sandbox Code Playgroud)
// user.js
var schema = new Schema({
location: {
type: pointSchema,
},
...
});
schema.index({ location: '2dsphere' });
var User = mongoose.model('User', schema);
Run Code Online (Sandbox Code Playgroud)
// routeHandler.js
const near = { type: 'Point', coordinates: [lng, …
Run Code Online (Sandbox Code Playgroud) 我在发布数据更新期间遇到猫鼬错误。错误是:
MongoError:无法提取地理密钥
我尝试通过谷歌搜索找到原因和解决方案,但仍然没有得到任何正确的解决方案。
帖子模型
const geoLocationSchema = new mongoose.Schema({
type: { type: String, default: 'Point'},
coordinates: {
type: [Number],
index: { type: '2dsphere', sparse: false },
}
});
const postsSchema = new mongoose.Schema({
post_title: String,
geoLocation: geoLocationSchema,
}, {
timestamps: true
});
Run Code Online (Sandbox Code Playgroud)
后置控制器
const Posts = require("../models/Posts");
var findPattern = {_id: "5e8c661f274e8e57d8e03887"};
var updatePattern = { geoLocation: {
type: "Point",
coordinates: [-8.4556973, 114.5110151] }
};
Posts.updateOne(findPattern, updatePattern) .then(updateRes => {
console.log("post updated");
}).catch(err => {
console.log("error", err.toString());
});
Run Code Online (Sandbox Code Playgroud)
输出是:
MongoError:无法提取地理键:{_id:ObjectId('5e8c661f274e8e57d8e03887'),geoLocation:{类型:“点”,坐标:[-8.455697300000001,114.5110151],_id:ObjectId('5e8d7f7c35b9d36d45b48 …
位置点另存为
{
"location_point" : {
"coordinates" : [
-95.712891,
37.09024
],
"type" : "Point"
},
"location_point" : {
"coordinates" : [
-95.712893,
37.09024
],
"type" : "Point"
},
"location_point" : {
"coordinates" : [
-85.712883,
37.09024
],
"type" : "Point"
},
.......
.......
}
Run Code Online (Sandbox Code Playgroud)
有几个文件。我需要到group
最近的地点。分组后第一个第二个位置将在一个文档中,第三个在第二个文档中。请注意,第一和第二的位置点不相等。两个都是最近的地方。
有什么办法吗?提前致谢。