No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Consultas geoespaciales con $geoNear

13/21
Recursos

Aportes 3

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

El reto:
Dentro del operador “geoNear” utilice query para filtrar lo que estos buscando.

[
  {
    $geoNear: {
      near: {
        type: "Point",
        //coordinates: [-73.95553399775919, 40.79964571639598],
        coordinates: [-155.98514, 19.61952]
      },
      distanceField: "distancia",
      maxDistance: 30000000,
      query: {neighborhood_overview:{$regex:"^[oO]+[cC]+[a-zA-Z]+",$options:'i'}},
      //includeLocs: "",
      //num: number,
      spherical: true,
    },
  },
  {
    $match:{
      beds:{$ne:0}
    }
  },
  {
    $project: {
      distancia: 1,
      suburb: "$address.suburb",
      government_area:"$address.government_area",
      precioxCama:{
        $divide:["$price", "$beds"]
      }
    }
  },
  {
    $group:{
      _id: {
        $cond:{
          if:{
            $lte:["$distancia", 2000]
          },
          then:"menos de 2K",
          else:"mas de 2k"
        } 
      },
      suburb:{"$first":"$suburb"},
      government_area: {"$first":"$government_area"},
      mediaxCama:{
        $avg: "$precioxCama"
      } 
    }
  }
]

#Resultado
_id: "mas de 2k"
suburb: ""
government_area: "Matosinhos e Leça da Palmeira"
mediaxCama: 21.25

_id: "menos de 2K"
suburb: "Kailua/Kona"
government_area: "North Kona"
mediaxCama: 55.00
use('sample_airbnb')

db.listingsAndReviews.aggregate([
  {
    $geoNear: {
      near: {
        type: "Point",
        coordinates: [
          -73.95552676483872,
          40.7994839486901
        ]
      },
      distanceField: "distancia",
      maxDistance: 30000,
      spherical: true
      }
},
  {
    $match: {
      beds: {
        $ne: 0
      }
    }
  },
  {
    $project: {
      _id:0,
      distancia: 1,
      precio : {
        $divide: ["$price", "$beds"]
    	}
    }
  },
  {
    $group: {
      _id: {
        $cond: {
          if:{
            $lte: ["$distancia", 2000]
          },
          then: "menos2K",
          else: "mas2K"
        }
      },
    	mediaPCama:{
        $avg: "$precio"
      }
    }
  }
  
])
Yo calculé todo adentro de un project ```js use('airbnb'); db.listingsAndReviews.createIndex( {"address.location": "2dsphere" }) db.listingsAndReviews.aggregate([ {$geoNear: { near: { type: 'Point', coordinates: [-157.83919,21.28634] }, distanceField: 'distance', maxDistance: 30000, }}, {$match: { "beds": {$ne:0 }, }}, {$project: { "priceByBed": {$toDouble: {$divide: ["$price", "$beds"]}}, "_id": 0, "distance":1, "isFar": { $gt: ["$distance", 200] } }}, {$group: { "_id": "$isFar", "averagePrice": { "$avg": "$priceByBed" } }}, {$project: { "_id": 0, "isFar": "$_id", "averagePrice": 1 }} ]) ```use('airbnb'); db.listingsAndReviews.createIndex( {"address.location": "2dsphere" }) db.listingsAndReviews.aggregate(\[ {$geoNear: { near: { type: 'Point', coordinates: \[-157.83919,21.28634] }, distanceField: 'distance', maxDistance: 30000, }}, {$match: { "beds": {$ne:0 }, }}, {$project: { "priceByBed": {$toDouble: {$divide: \["$price", "$beds"]}}, "\_id": 0, "distance":1, "isFar": { $gt: \["$distance", 200] } }}, {$group: { "\_id": "$isFar", "averagePrice": { "$avg": "$priceByBed" } }}, {$project: { "\_id": 0, "isFar": "$\_id", "averagePrice": 1 }}])