Related Topics
Overview Of MongoDB
MongoDB Page 1
MongoDB Page 2
MongoDB Page 3
No SQl Database
MongoDB Page 4
MongoDB Page 5
Advantages Over RDBMS
MongoDB Page 6
MongoDB Page 7
MongoDB Data Types
MongoDB Page 8
MongoDB Data Modeling
MongoDB Page 9
Query & Projection Operator
MongoDB Page 10
MongoDB Page 11
MongoDB Update Operator
MongoDB Page 12
AggregationPipeline Stages
MongoDB Page 13
MongoDB Page 14
MongoDB Limit()
MongoDB Page 15
MongoDB Sort()
MongoDB Page 16
Query Modifiers
MongoDB Page 17
Aggregation Commands
MongoDB Page 18
Geospatial Command
MongoDB Page 19
Query and Write Operation Commands
MongoDB Page 20
Query Plan Cache Commands
MongoDB Page 21
Authentication Commands
MongoDB Page 22
Role Management Commands
MongoDB Page 23
Replication Command
MongoDB Page 24
Shading Commands
MongoDB Page 25
Session Commands
MongoDB Page 26
Create Database
MongoDB Page 27
Drop Database
MongoDB Page 28
Create Collection
MongoDB Page 29
Drop Collection
MongoDB Page 30
Inset Documents
MongoDB Page 31
Update Documents
MongoDB Page 32
Delete Documents
MongoDB Page 33
SQL to MongoDB Mapping
MongoDB Page 34
Introduction to React.js
React JS Page 1
React JS Page 2
React JS Page 3
Components in React.js
React JS Page 4
React JS Page 5
Virtual DOM in React.js
React JS Page 6
React JS Page 7
State and Props in React.js
React JS Page 8
React JS Page 9
React Router
React JS Page 10
React JS Page 11
React Hooks
React JS Page 12
React JS Page 13
Redux in React.js
React JS Page 14
React JS Page 15
Context API in React.js
React JS Page 16
React JS Page 17
React with Webpack and Babel
React JS Page 18
React JS Page 19
Testing in React.js
React JS Page 20
React JS Page 21
Deployment and Optimization in React.js
React JS Page 22
React JS Page 23
Emerging Trends and Best Practices in React.js
React JS Page 24
React JS Page 25
Introduction
Node.js Page 1
Node.js Page 2
Node.js Architecture and Event-Driven Programming
Node.js Page 3
Node.js Page 4
Modules and Packages in Node.js
Node.js Page 5
Node.js Page 6
File System and Buffers in Node.js
Node.js Page 7
Node.js Page 8
HTTP and Networking in Node.js
Node.js Page 9
Node.js Page 10
Express.js and Web Applications
Node.js Page 11
Node.js Page 12
Databases and ORMs in Node.js
Node.js Page 13
Node.js Page 14
RESTful APIs in Node.js
Node.js Page 15
Node.js Page 16
Testing and Debugging in Node.js
Node.js Page 17
Deployment and Scalability in Node.js
Node.js Page 18
Node.js Page 19
Emerging Trends and Best Practices in Node.js
Node.js Page 20
Node.js Page 21
Performance Optimization in Node.js
Node.js Page 22
Node.js Page 23

MongoDB
[
{ "_id": 1, "name": "Park", "location": { "type": "Point", "coordinates": [ -73.97, 40.77 ] } },
{ "_id": 2, "name": "Restaurant", "location": { "type": "Point", "coordinates": [ -73.98, 40.75 ] } },
{ "_id": 3, "name": "Museum", "location": { "type": "Point", "coordinates": [ -73.96, 40.78 ] } }
]
To find the locations near a specific point, you can use the $near
command:
db.locations.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [ -73.97, 40.76 ]
},
$maxDistance: 1000 // in meters
}
}
})
This query returns the locations within 1000 meters of the specified point.
You can also use the $geoWithin
command to find locations within a specific shape, such as a polygon:
db.locations.find({
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[ -73.98, 40.76 ],
[ -73.96, 40.76 ],
[ -73.96, 40.78 ],
[ -73.98, 40.78 ],
[ -73.98, 40.76 ]
]
]
}
}
}
})
This query returns the locations that are completely within the specified polygon.
Geospatial commands and features in MongoDB enable you to perform powerful geospatial queries and operations on your data, making it easier to work with location-based information in your applications.
[
{ "_id": 1, "name": "Park", "location": { "type": "Point", "coordinates": [ -73.97, 40.77 ] } },
{ "_id": 2, "name": "Restaurant", "location": { "type": "Point", "coordinates": [ -73.98, 40.75 ] } },
{ "_id": 3, "name": "Museum", "location": { "type": "Point", "coordinates": [ -73.96, 40.78 ] } }
]
To find the locations near a specific point using the $geoNear
stage:
db.locations.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: [ -73.97, 40.76 ]
},
distanceField: "distance",
spherical: true
}
}
])
In this example, the $geoNear
stage takes the following parameters:
near
: Specifies the point from which to calculate distances.distanceField
: Specifies the name of the field where the calculated distances will be stored.spherical
: Specifies whether to perform calculations on a sphere (true) or a flat surface (false).
The output of the aggregation will include the documents sorted by their proximity to the specified point, along with the calculated distance for each document.
It’s important to note that the $geoNear
stage requires a geospatial index on the field used for the geospatial query. Before using the $geoNear
stage, make sure to create a geospatial index on the relevant field using the createIndex()
method.
While the $geoNear
stage in the aggregation framework provides powerful geospatial capabilities, it’s worth mentioning that other geospatial operators and commands, such as $near
and $geoWithin
, may be more suitable for certain use cases.
[
{ "_id": 1, "name": "Park", "location": { "type": "Point", "coordinates": [ -73.97, 40.77 ] } },
{ "_id": 2, "name": "Restaurant", "location": { "type": "Point", "coordinates": [ -73.98, 40.75 ] } },
{ "_id": 3, "name": "Museum", "location": { "type": "Point", "coordinates": [ -73.96, 40.78 ] } }
]
To find locations within a specific shape, such as a polygon, you can use the $geoWithin
operator in a find()
query:
db.locations.find({
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[ -73.98, 40.76 ],
[ -73.96, 40.76 ],
[ -73.96, 40.78 ],
[ -73.98, 40.78 ],
[ -73.98, 40.76 ]
]
]
}
}
}
})
In this example, the location
field is specified with the $geoWithin
operator, which takes the $geometry
parameter. The $geometry
parameter defines the shape or geometry for the query. In this case, it represents a polygon with the specified coordinates.
The query will return the documents from the “locations” collection that are completely within the specified polygon.
The $geoWithin
operator can also be used within the aggregation framework, enabling you to perform more complex geospatial operations and calculations using the $match
stage.
The purpose of the $geoWithin
operator is to query for documents that are entirely contained within a specified area. It is useful for scenarios where you need to find locations within a specific shape, such as finding points within a polygon or finding areas within a larger region.
Note that for geospatial queries, you should have a geospatial index on the relevant field for efficient query performance. You can create a geospatial index using the createIndex()
method in MongoDB.
Overall, the $geoWithin
operator allows you to perform powerful geospatial queries in MongoDB to find documents that fall within a specified shape or geometry.
[
{ "_id": 1, "name": "Area A", "boundary": { "type": "Polygon", "coordinates": [[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]] } },
{ "_id": 2, "name": "Area B", "boundary": { "type": "Polygon", "coordinates": [[[5, 5], [5, 15], [15, 15], [15, 5], [5, 5]]] } },
{ "_id": 3, "name": "Area C", "boundary": { "type": "Polygon", "coordinates": [[[12, 12], [12, 20], [20, 20], [20, 12], [12, 12]]] } }
]
To find the areas that intersect with a specific shape, such as a polygon, you can use the $geoIntersects
operator in a find()
query:
db.areas.find({
boundary: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [
[
[ 3, 3 ],
[ 3, 8 ],
[ 8, 8 ],
[ 8, 3 ],
[ 3, 3 ]
]
]
}
}
}
})
In this example, the boundary
field is specified with the $geoIntersects
operator, which takes the $geometry
parameter. The $geometry
parameter defines the shape or geometry for the query. In this case, it represents a polygon with the specified coordinates.
The query will return the documents from the “areas” collection that intersect with the specified polygon.
The $geoIntersects
operator can also be used within the aggregation framework, enabling you to perform more complex geospatial operations and calculations using the $match
stage.
The purpose of the $geoIntersects
operator is to query for documents that have a spatial relationship with a specified area. It is useful for scenarios where you need to find documents that intersect or have any spatial relationship with a given shape, such as finding areas that overlap with a region or determining boundaries that touch a line.
Note that for geospatial queries, you should have a geospatial index on the relevant field for efficient query performance. You can create a geospatial index using the createIndex()
method in MongoDB.
Overall, the $geoIntersects
operator allows you to perform powerful geospatial queries in MongoDB to find documents that intersect with a specified shape or geometry.
[
{ "_id": 1, "name": "Park", "location": { "type": "Point", "coordinates": [ -73.97, 40.77 ] } },
{ "_id": 2, "name": "Restaurant", "location": { "type": "Point", "coordinates": [ -73.98, 40.75 ] } },
{ "_id": 3, "name": "Museum", "location": { "type": "Point", "coordinates": [ -73.96, 40.78 ] } }
]
To find locations near a specific point on the sphere using the $nearSphere
operator:
db.locations.find({
location: {
$nearSphere: {
$geometry: {
type: "Point",
coordinates: [ -73.97, 40.76 ]
},
$maxDistance: 1000 // Optional: Specifies the maximum distance in meters
}
}
})
In this example, the location
field is specified with the $nearSphere
operator, which takes the $geometry
parameter. The $geometry
parameter represents the point on the sphere to which you want to find nearby locations. The coordinates
property specifies the longitude and latitude of the point.
You can also include the optional $maxDistance
parameter, which specifies the maximum distance from the specified point. The distance is measured in meters. This parameter is useful for limiting the search to a specific radius or proximity.
The query will return the documents from the “locations” collection that are near the specified point on the sphere, sorted by their distance.
The $nearSphere
operator can also be used within the aggregation framework, allowing you to perform more complex geospatial operations and calculations using the $match
stage.
The purpose of the $nearSphere
operator is to query for documents based on their proximity to a specified point on the Earth’s surface, taking into account the curvature of the sphere. It is particularly useful for location-based services, mapping applications, and any scenario where you need to find nearby locations on a global scale.
Note that for geospatial queries, you should have a geospatial index on the relevant field for efficient query performance. You can create a geospatial index using the createIndex()
method in MongoDB.
Overall, the $nearSphere
operator allows you to perform geospatial queries in MongoDB to find documents near a specified point on the sphere, considering the Earth’s curvature.




Popular Category
Topics for You
Introduction to React.js
React JS Page 1
React JS Page 2
React JS Page 3
Components in React.js
React JS Page 4
React JS Page 5
Virtual DOM in React.js
React JS Page 6
React JS Page 7
State and Props in React.js
React JS Page 8
React JS Page 9
React Router
React JS Page 10
React JS Page 11
React Hooks
React JS Page 12
React JS Page 13
Redux in React.js
React JS Page 14
React JS Page 15
Context API in React.js
React JS Page 16
React JS Page 17
React with Webpack and Babel
React JS Page 18
React JS Page 19
Testing in React.js
React JS Page 20
React JS Page 21
Deployment and Optimization in React.js
React JS Page 22
React JS Page 23
Emerging Trends and Best Practices in React.js
React JS Page 24
React JS Page 25
Introduction
Node.js Page 1
Node.js Page 2
Node.js Architecture and Event-Driven Programming
Node.js Page 3
Node.js Page 4
Modules and Packages in Node.js
Node.js Page 5
Node.js Page 6
File System and Buffers in Node.js
Node.js Page 7
Node.js Page 8
HTTP and Networking in Node.js
Node.js Page 9
Node.js Page 10
Express.js and Web Applications
Node.js Page 11
Node.js Page 12
Databases and ORMs in Node.js
Node.js Page 13
Node.js Page 14
RESTful APIs in Node.js
Node.js Page 15
Node.js Page 16
Testing and Debugging in Node.js
Node.js Page 17
Deployment and Scalability in Node.js
Node.js Page 18
Node.js Page 19
Emerging Trends and Best Practices in Node.js
Node.js Page 20
Node.js Page 21
Performance Optimization in Node.js
Node.js Page 22
Node.js Page 23
Go through our study material. Your Job is awaiting.