I’m trying to retrieve one specific object from an existing array inside the DB document, using wildcards $near
and $geometry
.
I have a model that looks like this:
const schema = new mongoose.Schema({
name: {...},
email: {...},
...
locations: [
{
name: String,
address: String,
mapurl: String,
location: {
type: {
type: String,
default: "Point",
},
coordinates: [
{
type: Number,
},
],
},
},
],
}
Schema indexed like that:
schema.index({ "locations.location": "2dsphere" });
Document in DB looks like this:
{
"_id": ...,
"name": ...,
"email": ...,
"locations": [
{
"name": "name 1",
"address": "address 1",
"mapurl": "https://maps.google.com/SOME_MAP_URL_1",
"location": {
"type": "Point",
"coordinates": [
49.8370513,
24.0311641
]
},
"_id": {
"$oid": "..."
}
},
{
"name": "name 2",
"address": "address 2",
"mapurl": "https://maps.google.com/SOME_MAP_URL_2",
"location": {
"type": "Point",
"coordinates": [
49.83454210000001,
24.0115613
]
},
"_id": {
"$oid": "..."
}
},
{
"name": "name 3",
"address": "address 3",
"mapurl": "https://maps.google.com/SOME_MAP_URL_3",
"location": {
"type": "Point",
"coordinates": [
49.81605019999999,
24.0016884
]
},
"_id": {
"$oid": "..."
}
},
],
}
I’m trying to query DB for a specific Object in an Array and then get all info I need from that Object with .select()
:
const q = {
_id: place._id,
"locations.location": {
$near: {
$geometry: {
type: "Point",
coordinates: [49.837066, 24.031458]
},
$maxDistance: 100, // 100m
},
},
};
const storeCoords = await Place.find(q).select("locations.location");
As the result I’m expecting to get only one specific Object from Array that is near 100 meters from provided coordinates, but instead I’m getting the whole Array with all existing Object there, including the one I need.
Expecting result:
{
"coords": [
{
"_id": "...",
"locations": [
{
"location": {
"type": "Point",
"coordinates": [49.83454210000001, 24.0115613]
}
},
]
}
]
}
Actual result:
{
"coords": [
{
"_id": "...",
"locations": [
{
"location": {
"type": "Point",
"coordinates": [49.8370513, 24.0311641]
}
},
{
"location": {
"type": "Point",
"coordinates": [49.83454210000001, 24.0115613]
}
},
{
"location": {
"type": "Point",
"coordinates": [49.81605019999999, 24.0016884]
}
}
]
}
]
}
I might use that $near
and $geometry
in a wrong way or completly wrong wildcards.
Please, advise.
2
Answers
Okay, so eventually I've figured out the solution. Thanks @SAMRAT for suggestions.
I've created "statics"/"class" method to the Model itself, that returns an aggregated data:
And with the help of this Model function/method I can simply call it inside my controller with required parametres as follows:
const location = await Place.getLocation(id, coordinates);
Final result returns what's expected.
Besides that I've modified Model Schema to look like this:
So I can retrieve all other related information as
name
,mapurl
etc.If you only want to get only one document then you should use findOne.
But in your case, it will not give you the answer you are expecting because findOne will return the 1st document match to the condition from your DB which may not be the nearest one.
In that case, you should use the ‘$geoNear’ operator.
Here is the aggregation that may help