MongoDB官方文档$geoNear聚合管道示例报错,求正确实现方法
MongoDB $geoNear聚合查询报错解决:$geoNear requires a 'near' option as an Array
问题描述
我在查阅MongoDB官方文档的$geoNear示例时,遇到代码无法运行的情况,报错信息为:$geoNear requires a 'near' option as an Array
官方示例插入代码:
db.places.insertMany( [ { name: "Central Park", location: { type: "Point", coordinates: [ -73.97, 40.77 ] }, category: "Parks" }, { name: "Sara D. Roosevelt Park", location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] }, category: "Parks" }, { name: "Polo Grounds", location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] }, category: "Stadiums" } ] )
报错的聚合查询代码:
db.places.aggregate( [ { $lookup: { from: "places", let: { pt: "$location" }, pipeline: [ { $geoNear: { near: "$$pt", distanceField: "distance" } } ], as: "joinedField" } }, { $match: { name: "Sara D. Roosevelt Park" } } ] );
补充:我已在MongoDB Compass中为center字段创建2dsphere索引,测试代码如下(VS Code Playground):
use('hello'); db.world.drop(); db.world.insertMany([ { 'PRSN': '099HP0002', 'address': 'nan', 'applicantCategory': 'company', 'applicantName': 'companyNameA', 'buildingAdmins': 'nan', 'buildingOwners': 'nan', 'center': {'coordinates': [120.36521, 23.20558], 'type': 'Point'}, 'georeferencedParcels': ['433300860001'], }, { 'PRSN': '099HP0005', 'address': 'nan', 'applicantCategory': 'company', 'applicantName': 'companyNameB', 'buildingAdmins': 'nan', 'buildingOwners': 'nan', 'center': {'coordinates': [120.33333, 23.20555], 'type': 'Point'}, 'georeferencedParcels': ['433300860004'], }, { 'PRSN': '099HP0008', 'address': 'nan', 'applicantCategory': 'company', 'applicantName': 'companyNameC', 'buildingAdmins': 'nan', 'buildingOwners': 'nan', 'center': {'coordinates': [120.40000, 23.20551], 'type': 'Point'}, 'georeferencedParcels': ['433300860004'], } ]); db.world.createIndex( { center: "2dsphere" } ) db.world.aggregate([ { $lookup: { from: "world", let: { pt: "$center" }, pipeline: [ { $geoNear: { near: "$$pt", distanceField: "distance" } } ], as: "joinedField" } }, { $match: { name: "companyNameB" } } ])
正确实现方式
1. 修正官方示例的聚合代码
db.places.aggregate([ { $lookup: { from: "places", let: { pt_coords: "$location.coordinates" }, pipeline: [ { $geoNear: { near: "$$pt_coords", distanceField: "distance", spherical: true } } ], as: "joinedField" } }, { $match: { name: "Sara D. Roosevelt Park" } } ]);
2. 修正你的测试代码
use('hello'); db.world.drop(); db.world.insertMany([ { 'PRSN': '099HP0002', 'address': 'nan', 'applicantCategory': 'company', 'applicantName': 'companyNameA', 'buildingAdmins': 'nan', 'buildingOwners': 'nan', 'center': {'coordinates': [120.36521, 23.20558], 'type': 'Point'}, 'georeferencedParcels': ['433300860001'], }, { 'PRSN': '099HP0005', 'address': 'nan', 'applicantCategory': 'company', 'applicantName': 'companyNameB', 'buildingAdmins': 'nan', 'buildingOwners': 'nan', 'center': {'coordinates': [120.33333, 23.20555], 'type': 'Point'}, 'georeferencedParcels': ['433300860004'], }, { 'PRSN': '099HP0008', 'address': 'nan', 'applicantCategory': 'company', 'applicantName': 'companyNameC', 'buildingAdmins': 'nan', 'buildingOwners': 'nan', 'center': {'coordinates': [120.40000, 23.20551], 'type': 'Point'}, 'georeferencedParcels': ['433300860004'], } ]); db.world.createIndex( { center: "2dsphere" } ) db.world.aggregate([ { $lookup: { from: "world", let: { pt_coords: "$center.coordinates" }, pipeline: [ { $geoNear: { near: "$$pt_coords", distanceField: "distance", spherical: true } } ], as: "joinedField" } }, { $match: { applicantName: "companyNameB" // 修正字段名:原文档字段是applicantName而非name } } ])
关键修正点
- $geoNear参数调整:不能直接传入完整的GeoJSON对象(如
$$pt),需提取其中的coordinates数组作为near的值;使用2dsphere索引时,必须添加spherical: true启用球面距离计算。 - 字段名修正:你的测试代码中
$match条件误用了name字段,实际文档中对应的字段是applicantName,这会导致查询无结果。
内容的提问来源于stack exchange,提问作者Cookie
相关产品推荐
相关产品推荐

