我想在mongoDB上运行查询,并在查询中使用$near
。下面的查询是可行的,但我需要根据LAT和LNG进行累加。我正在尝试根据距pickle_location一定距离内的那些进行聚合或计数。
我正在使用下面的shell,NOT代码。
该处是一个记录:
{
"_id": {
"$oid": "5445ab058767000062"
},
"comment": null,
"dropoff_address": "XHgwMmB7fk1lRn59YFx4MDM=U2FsdGVkX19s3u4NEtImfofJzxnGreGpsna8qA4uVrq7exRDVy+zPn5UwDOj\nzpIs",
"dropoff_location": [
-97.816991,
30.189151
],
"pickup_address": "XHgwMmB7fk1lRn59YFx4MDM=U2FsdGVkX1/23mD3Vv3Nyf4/t+AEickIgOlkaxVp5y/e/5Ia2d3Z0OXtnejw\nrOK+ZPvxQontA9SS30t+MbUIrCMhndxpYcKNFm4xfOzRVxM=",
"pickup_location": [
-97.82075191025548,
30.20993147664687
],
"scheduled_request": false,
"status": "blah",
"timestamp_requested": {
"$date": "2014-10-21T00:38:28.990Z"
},
"total_owed_in_cents": 0,
"total_received_from_in_cents": 0,
"user_id": "5445a9000057"
}
工作的:
db.thing_requests.aggregate(
[
{$match: {total_received_in_cents: {$gt:1800}, requested_type: 'Blah' }},
{
$group:
{
_id: null,
average: { $avg: "$total_received_in_cents" }
}
}
]
)
需要使用这个添加到哪里上述工程:
{
the_location: {
$near: {
$geometry: {
type: "Point" ,
coordinates: [ <longitude> , <latitude> ]
},
$maxDistance: <distance in meters>,
$minDistance: <distance in meters>
}
}
}
UPDATE:
顶部查询起作用。我需要的是说所有被汇总的物品,我也需要确保他们是靠近LNG的某个LAT &。
更新2:
然此查询
db.thing_requests.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ -97.888,37.3222 ] },
distanceField: "dist.calculated",
maxDistance: 2,
minDistance :1,
query: {total_received_in_cents: {$gt:1800}, requested_type: 'Blah' },
includeLocs: "dist.location",
num: 5,
spherical: true
}
},
{
$group:
{
_id: "$user_id",
average: { $avg: "$total_received_from_requester_in_cents" }
}
}
])
收到此错误:
assert: command failed: {
"errmsg" : "exception: geoNear command failed: { ok: 0.0, errmsg: \"no geo indices for geoNear\" }",
"code" : 16604,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: geoNear command failed: { ok: 0.0, errmsg: \"no geo indices for geoNear\" }",
"code" : 16604,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
at (shell):1:23
2017-08-28T21:21:40.153-0500 E QUERY Error: command failed: {
"errmsg" : "exception: geoNear command failed: { ok: 0.0, errmsg: \"no geo indices for geoNear\" }",
"code" : 16604,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
at (shell):1:23 at src/mongo/shell/assert.js:13
你是什么意思的“添加”?除了** first **之外,您不能在任何其他管道阶段使用'$ near',因为它需要一个索引**,然后真的很可能需要['$ geoNear'](https://docs.mongodb .com/manual/reference/operator/aggregation/geoNear /),因为这是一个特殊的流水线阶段,实际上将“距离”作为添加到文档中的值返回,当您进一步操作结果并可能更改订单返回。您应该显示一小部分数据样本以及您期望实现的结果。 –
我更新以说明我在找什么。 – jdog
听起来像'$ geoNear',另一个查询被添加到该流水线阶段的'query'选项中。您还可以简单地在'$ match'中包含'$ near'和其他条件,因为如果您只是想要从返回的文档中汇总平均值,您并不需要**距离输出。请注意,“有区别”是因为'$ geoNear'仅在默认情况下返回'100'文档。你可以“调整”,但如果你有一个不确定的数字,距离值并不重要,那么你应该加上'$ near'。 –