$geoNear matching nearest array in this question suggested to use $geoNear and its option "includeLocs". However, this aggregate is unsupported in MongoDb Stitch function. You can read in documentation https://docs.mongodb.com/stitch/mongodb/actions/collection.aggregate/#unsupported-aggregation-stages
In MongoDb Stitch I can easily query with geospatial, using $geoWithin. But I can't separate my return model. Is there any alternative "includeLocs" or how can I filter $geoWithin result?
My model is:
[
{
"locations": [
{
"address": "bla bla bla",
"city": "Avila Beach",
"coordinates": [
-120.73605,
35.17998
],
"country": "Usa",
"prods": [
{
"brand": "kumcho",
"cagtegory": "tire",
"id": "1_kumcho"
},
{
"brand": "micheline",
"cagtegory": "tire",
"id": "1_micheline"
},
{
"brand": "setrr",
"cagtegory": "rim",
"id": "1_setrr"
}
],
"state": "5 Cities"
},
{
"address": "data data data",
"city": "Arvin",
"coordinates": [
-118.84151,
35.21617
],
"country": "Usa",
"prods": [
],
"state": "Bakersfield",
}
],
"name": "My Car",
"admin": "John"
}
]
According to this model, When I query like below.
.find({"locations.coordinates": {$geoWithin: {$centerSphere: [[-120.55361687164304, 35.22037830812648], 15/3963.2]}}},{"locations.coordinates":1})
.toArray()
Aggregate alternative.
const pipeline = [
{$match: {"locations.coordinates": {$geoWithin: {$centerSphere: [[-120.55361687164304, 35.22037830812648], 15/3963.2]}}}}
];
const cursor = coll.aggregate(pipeline);
the result is:
[
{
"locations": [
{
"coordinates": [
{
"$numberDouble": "-120.73605"
},
{
"$numberDouble": "35.17998"
}
]
},
{
"coordinates": [
{
"$numberDouble": "-118.84151"
},
{
"$numberDouble": "35.21617"
}
]
}
]
}
]
But I want specific array object of the document with root, which match with the searched coordinates.
For example, I need something like that.
Search values:
lon = -120.55361687164304
lat = 35.22037830812648
kmml = 3963.2
(this is miles)
distance = 15
According to search values above, the matched model coordinates is:
"coordinates": [-120.73605, 35.17998]
So I want result something like that:
[
{
"locations": [
{
"address": "bla bla bla",
"city": "Avila Beach",
"coordinates": [
-120.73605,
35.17998
],
"country": "Usa",
"prods": [
{
"brand": "kumcho",
"cagtegory": "tire",
"id": "1_kumcho"
},
{
"brand": "micheline",
"cagtegory": "tire",
"id": "1_micheline"
},
{
"brand": "setrr",
"cagtegory": "rim",
"id": "1_setrr"
}
],
"state": "5 Cities"
}
],
"name": "My Car",
"admin": "John"
}
]
is it possible return specific fields in mongodb document?
Thank you.
geoNear Deprecated Since Version 4.0. You can try to use $near $geometry; https://docs.mongodb.com/manual/reference/operator/query/near/
//Mile-Km to meters
if (kmml == 3963.2){
distance = distance * 1609.344;
} else{
distance = distance * 1000;
}
return coll
.find({"location":{
$near: {
$geometry: {
type : "Point",
coordinates : [ lon, lat ]
},
$maxDistance: distance
}
}
}).toArray();