Hi everyone!
I’m trying to perform a $geoWithin query on the location field of the last item of an address array that looks like below…
address: [
{
street: "4 Pivet Drive",
city: "Little Whinging",
state: "Surrey",
location: {
type: "Point",
coordinates: [1, 1]
}
},
{
street: "Hogwarts Castle",
city: "Scottish Highlands",
state: "London",
location: {
type: "Point",
coordinates: [2, 2]
}
},
]
I’m using nodejs and mongoose, and below is how I wrote the code.
const result = await User.findOne(
{
_id: userId,
"address.-1.location": {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [[[0, 0], [0, 4], [4, 4], [4, 0], [0, 0]]],
},
},
},
}
);
The result is null.
What is the correct way to implement this or will the query not work on an array?
Thank you.