$nearSphere - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$nearSphere

$nearSphereOperator di Amazon DocumentDB digunakan untuk menemukan dokumen yang berada dalam jarak tertentu dari titik geospasial. Operator ini sangat berguna untuk kueri geo-spasial, seperti menemukan semua restoran dalam radius tertentu dari lokasi tertentu.

Parameter

  • $geometry: Sebuah objek GeoJSON yang mewakili titik referensi. Harus menjadi Point objek dengan type dan coordinates bidang.

  • $minDistance: (opsional) Jarak minimum (dalam meter) dari titik referensi yang harus dimiliki dokumen.

  • $maxDistance: (opsional) Jarak maksimum (dalam meter) dari titik referensi yang harus dimiliki dokumen.

Contoh (MongoDB Shell)

Dalam contoh ini, kita akan menemukan semua restoran dalam jarak 2 kilometer (2000 meter) dari lokasi tertentu di Seattle, WA.

Buat dokumen sampel

db.usarestaurants.insert([ { name: "Noodle House", location: { type: "Point", coordinates: [-122.3516, 47.6156] } }, { name: "Pike Place Grill", location: { type: "Point", coordinates: [-122.3403, 47.6101] } }, { name: "Seattle Coffee Co.", location: { type: "Point", coordinates: [-122.3339, 47.6062] } } ]);

Contoh kueri

db.usarestaurants.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [-122.3516, 47.6156] }, $minDistance: 1, $maxDistance: 2000 } } }, { name: 1 });

Keluaran

{ "_id" : ObjectId("611f3da985009a81ad38e74b"), "name" : "Noodle House" } { "_id" : ObjectId("611f3da985009a81ad38e74c"), "name" : "Pike Place Grill" }

Contoh kode

Untuk melihat contoh kode untuk menggunakan $nearSphere perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

Node.js
const { MongoClient } = require('mongodb'); async function findNearbyRestaurants() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const restaurants = db.collection('usarestaurants'); const result = await restaurants.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [-122.3516, 47.6156] }, $minDistance: 1, $maxDistance: 2000 } } }, { projection: { name: 1 } }).toArray(); console.log(result); client.close(); } findNearbyRestaurants();
Python
from pymongo import MongoClient def find_nearby_restaurants(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test restaurants = db.usarestaurants result = list(restaurants.find({ 'location': { '$nearSphere': { '$geometry': { 'type': 'Point', 'coordinates': [-122.3516, 47.6156] }, '$minDistance': 1, '$maxDistance': 2000 } } }, { 'name': 1 })) print(result) client.close() find_nearby_restaurants()