db.data.aggregate([
{
$geoNear:{
near:{
type:"Point",
coordinates:[
10.7xxxxx,
52.4xxxxx
]
},
distanceField:"dist.calculated",
maxDistance:3900,
num:50000000,
includeLocs:"dist.location",
spherical:true
}
}
])
How can I get this query working nonetheless?
Hi Koko,
I have found out that the mongod logs show the following error: “too many results for query …, truncating output” which means that the maximum document size of 16mb gets exceeded.
How can I get this query working nonetheless?
Currently, the “geoNear” aggregation stage is limited to return results that are within the 16MB BSON size limit. This is related to an issue with earlier version of MongoDB (which is described in https://jira.mongodb.org/browse/SERVER-13486). Your query hit this issue because “geoNear” returns a single document (contains an array of result documents) and the “allowDiskUse” aggregation pipeline option unfortunately does not help in this case.
There are two options that could be considered:
num, limit, or maxDistance optionsfind() operator which is not limited to the BSON maximum size since it returns a cursor.Below is a test I done on MongoDB 3.2.10 For your information.
Create “2dsphere” for designated collection:
db.coll.createIndex({location: '2dsphere'})
Create and insert several big documents.
var padding = '';
for (var j = 0; j < 15; j++) {
for (var i = 1024*128; i > 0; --i) {
var padding = padding + '12345678';
}
}
db.coll.insert({location:{type:"Point", coordinates:[-73.861, 40.73]}, padding:padding})
db.coll.insert({location:{type:"Point", coordinates:[-73.862, 40.73]}, padding:padding})
db.coll.insert({location:{type:"Point", coordinates:[-73.863, 40.73]}, padding:padding})
db.coll.insert({location:{type:"Point", coordinates:[-73.864, 40.73]}, padding:padding})
db.coll.insert({location:{type:"Point", coordinates:[-73.865, 40.73]}, padding:padding})
db.coll.insert({location:{type:"Point", coordinates:[-73.866, 40.73]}, padding:padding})
Query using “geoNear” and server log shows “Too many geoNear results …, truncating output”
db.coll.aggregate(
[
{
$geoNear:{
near:{type:"Point", coordinates:[-73.86, 40.73]},
distanceField:"dist.calculated",
maxDistance:150000000,
spherical:true
}
},
{$project: {location:1}}
]
)
Query using “find” and all expected documents are returned
// This and following "var" are necessary to avoid the screen being flushed by padding string.
var cursor = db.coll.find (
{
location: {
$near: {
$geometry:{type:"Point", coordinates:[-73.86, 40.73]},
maxDistance:150000,
}
}
}
)
// It is necessary to iterate through the cursor. Otherwise, the query is not actually executed.
var x = cursor.next()
x._id
var x = cursor.next()
x._id
...
Regards,
Lungang
Error: error: {
"code" : 13501,
"ok" : 0,
"errmsg" : "use geoNear command rather than $near query"
}