我一直试图通过猫鼬来确定忍者与geoLocation点之间的距离。我已经尝试了所有的东西,但不知怎的,我找不到Mongoose的正确文档,这些是一些新的变化。
像这样:
const express = require('express');
const router = express.Router();
const Ninja = require('../models/ninja');
// get a list of ninjas from the database
router.get('/ninjas', (req, res, next) => {
Ninja.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ parseFloat(req.query.lng) , parseFloat(req.query.lat) ] },
maxDistance: 100000,
distanceField: "distance",
spherical: true
}
}
]).then(function(ninjas){
res.send(ninjas);
}).catch(next)
});

但是,当我通过Postman将params添加到URL时,我找不到它。
这是我的架构:
const mongoose = require ('mongoose');
const Schema = mongoose.Schema;
// CREATE GEOLOCATION SCHEMA
const GeoSchema = new Schema({
type:{
type: String,
default: "Point"
},
coordinates: {
type: [Number],
index: "2dsphere"
}
});
// CREATE NINJA SCHEMA AND MODEL
const NinjaSchema = new Schema({
"name": {
type: String,
required: [true, 'Name field is required']
},
"rank": {
type: String,
},
"available": {
type: Boolean,
default: false
},
"geometry": GeoSchema
});
const Ninja = mongoose.model('ninja', NinjaSchema);
module.exports = Ninja;

答案 0 :(得分:0)
更正您的忍者模型和模式代码
// create ninja Schema & model
const NinjaSchema = new Schema({
name: {
type: String,
required: [true, 'Name field is required']
},
rank: {
type: String
},
available: {
type: Boolean,
default: false
},
geometry: GeoSchema
});