mongo-db
  1. mongo-db-geospatial-command

Geospatial Command - (Database and Collection Commands)

The Geospatial command in MongoDB allows you to perform geospatial queries on data contained within collections. This page will discuss the syntax, examples, output, explanation, use, important points, and summary of the Geospatial command.

Syntax

The syntax for using the Geospatial command in MongoDB is as follows:

db.collection.find({ loc: { $geoWithin: { $geometry: geometryObject } } })

Here, loc is the field containing the geospatial data, $geoWithin is the operator used for geospatial queries, and $geometry is the object that describes the area to be searched.

Example

Suppose we have a collection named stores with the following sample data:

{
  _id: ObjectId("60aac0f275487a8af84e82aa"),
  name: 'Store A',
  loc: {
    type: "Point",
    coordinates: [10.100, 20.000]
  }
},
{
  _id: ObjectId("60aac0f275487a8af84e82ab"),
  name: 'Store B',
  loc: {
    type: "Point",
    coordinates: [15.100, 25.000]
  }
},
...

To find all the stores that are within a certain distance from a given location, you can use the following query:

db.stores.find({ loc: { $geoWithin: { $centerSphere: [ [longitude, latitude], radius ] } } })

In this query, $centerSphere is a geospatial query operator that specifies a circular query area, longitude and latitude are the coordinates of the reference point, and radius is the query radius in radians.

For example, to find all stores within a 10km radius of the location (20.000, 30.000), you can execute the following command:

db.stores.find({ loc: { $geoWithin: { $centerSphere: [ [20.000, 30.000], 10 / 6378.1 ] } } })

Output

The output of the Geospatial command is a cursor that returns all the documents in the collection that match the query criteria specified in the command.

Explanation

The Geospatial command in MongoDB allows you to perform geospatial queries on data contained within collections. This is particularly useful for applications that require location-based data, such as geocoding, mapping, and spatial analytics.

The Geospatial command supports various geospatial query operators, such as $near, $geoIntersects, $geoWithin, and $centerSphere, among others. These operators allow you to perform queries that find documents based on their location and proximity to specified coordinates.

Use

The Geospatial command is used to perform geospatial queries on data contained within MongoDB collections. This command is particularly useful for applications that require location-based data, such as geocoding, mapping, and spatial analytics.

Important Points

  • The Geospatial command in MongoDB allows you to perform geospatial queries on data contained within collections.
  • The Geospatial command supports various geospatial query operators, such as $near, $geoIntersects, $geoWithin, and $centerSphere, among others.
  • The $centerSphere operator is used to specify a circular query area, longitude and latitude are the coordinates of the reference point, and radius is the query radius in radians.

Summary

In this page, we discussed the syntax, example, output, explanation, use, important points, and summary of the Geospatial command in MongoDB. The Geospatial command allows you to perform geospatial queries on data contained within collections, and supports various geospatial query operators. By using the Geospatial command in MongoDB, you can perform queries that find documents based on their location and proximity to specified coordinates, making it particularly useful for applications that require location-based data, such as geocoding, mapping, and spatial analytics.

Published on: