Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142646

Using $Geonear with two different documents

$
0
0

Im trying to get the information of my Vehicles that are inside or nearby a certain place.

db.getCollection('Vehicles').aggregate([
   {
     $lookup:
       {
         from: "Places",
         localField: "place",
         foreignField: "_id",
         as: "veiculo"
       }
  },
  {$unwind: "$veiculo"},
  {$match: 
      {
          $or: [{"veiculo.range": 200}, {"veiculo.coordinates": [41.530735, -8.621205]}]
      }
      }
])

I started using lookup but this matches the coordinates but wont use the range field that i want.

TO be on the coordinates or in a close distance.

{
    "_id" : ObjectId("5df8bd4d79ca3b3ecd23bf42"),
    "code" : NumberLong(4),
    "description" : "bike",
    "place" : ObjectId("5df8b38f79ca3b3ecd23bf41")
}

This is my vehicle schema

{
    "_id" : ObjectId("5df8b38f79ca3b3ecd23bf41"),
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            41.530735, 
            -8.621205
        ]
    },
    "range" : 200,
    "capacity" : 200,
    "quantity" : 100
}

This is my Place schema. Any help ?

EDIT:

Tried this

db.getCollection('Vehicles').aggregate([
   {
     $lookup:
       {
         from: "Places",
         localField: "place",
         foreignField: "_id",
         as: "veiculo"
       }
  },
  {$unwind: "$veiculo"},
  {"veiculo.location": {
    "$nearSphere": {
      "$geometry": {
        "type": "Point",
        "coordinates": coordinates
      },
      "$maxDistance": 1000
    }
  }
      }
])
db.getCollection('Places').aggregate({location: {
        $geoNear: {
          near: {
            type: 'Point',
            coordinates: [41.53035, -8.621205] //req.params: uri params
          },
                distanceField: "dist.calculated",
                maxDistance: 2,
                minDistance :1,
                includeLocs: "dist.location",
                num: 5,
                spherical: true
        }
      }},
      {
     $lookup:
       {
         from: "Vehicles",
         localField: "_id",
         foreignField: "place",
         as: "veiculo"
       }
  },  {$unwind: "$veiculo"}
      )

both dont work


Viewing all articles
Browse latest Browse all 142646

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>