Accessing array inside of an object

I hace the following:
location": {
“coordinates”: [-70.09639117297635, 44.02152086199026],
“type”: “Point”

My pipeline is:

  let resultData = await geoModel.aggregate([{
    $geoNear: {
      near: { "type": "location.Point", "location.coordinates": [-70.33013568836199, 43.63477528143518 ] },
      distanceField: "dist.calculated",
      maxDistance: 64373.8,
      query: { storeManager: "Sanket" },
      includeLocs: "dist.location",
      spheical: true

This gives me an error. If I remove “location” from coordinates, it works!!

What wrong with location.cordinates??

Hello @Chris_Job1 ,

As shown in this $geoNear documentation, the near field is described as the point for which to find the closest documents. If using a 2dsphere index, you can specify the point as either a GeoJSON point or legacy coordinate pair. If using a 2d index, specify the point as a legacy coordinate pair. The correct syntax to add coordinates in near filed is

near: { type: “Point”, coordinates: [ X , Y ] }
where X and Y are coordinates

I think that you are confusing this with accessing the objects of array and that is a different operation.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.