

Abstract
gRPC (gRPC Remote Procedure Call) is a high-performance, open-source remote procedure call (RPC) framework developed by Google basically to connect with a large number of microservices. It uses Protocol Buffers, a binary serialization format, to define and serialize APIs and messages sent between client and server applications.
Developers can define services using Protocol Buffers, and gRPC generates server and client code in multiple languages, making it easier to create distributed applications that can communicate with each other seamlessly. One key feature is its support for bi-directional streaming, which can result in significant performance improvements
Solution Details
This solution demonstrates how to set up a basic gRPC communication system using Node.js. It involves creating both a server and a client based on Protocol Buffers to facilitate efficient and strongly-typed data exchange.
What is Geospatial data?
Geospatial data refers to information linked to specific locations on the Earth’s surface, often represented by coordinates such as latitude and longitude.
What is Geo-Indexing?
Geo-indexing refers to the process of indexing geospatial data in a database to optimize the retrieval of queries based on geographical locations. In MongoDB, geo-indexes allow the database to efficiently perform queries that involve spatial locations such as finding all documents within a certain distance from a point, or within a specific geographical area.
Types of Geo-Indexes in MongoDB
MongoDB supports two main types of geo-indexes:
- 2D Index: Ideal for applications that deal with flat geometry (like maps of cities or small countries). This index supports queries on planar geometry.
- 2DSphere Index: Used for applications requiring indexing on a spherical surface (like the globe). This index type is essential for global geospatial queries and supports both GeoJSON and legacy coordinate pairs.

Code/Implementation Steps
To create a geo-index in MongoDB, you first need to ensure your documents contain geospatial data, typically formatted as GeoJSON objects or legacy coordinate pairs (longitude, latitude). Here’s a quick guide on setting up a 2DSphere index, which is commonly used for handling complex geographical queries.
{
"location": {
"type": "Point",
"coordinates": [-73.97, 40.77]
}
}
You can create a 2DSphere index on this document using the following MongoDB command:
db.collection.createIndex({ location: "2dsphere" })
-
Querying with Geo-Indexes
MongoDB offers a range of geospatial query operators that make use of geo-indexes. Some of the most commonly used are:
- $geoWithin: Finds documents within a certain geometric shape.
- $near: Finds documents near a specific point, providing results sorted by distance.
- $geoIntersects: Finds documents where the given geometry intersects with the document’s geometry.
Here is an example of a query that finds all locations within 100 meters of a point:
db.collection.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-73.97, 40.77],
},
$maxDistance: 100,
},
},
});
-
Setting up with client
Setting up the client involves creating a stub to call the remote service method and sending the request to the server.
const grpc = require("@grpc/grpc-js"); const protoLoader = require("@grpc/proto-loader"); const path = require("path"); const PROTO_PATH = path.join(__dirname, "definitions/test.proto"); const protoDefinition = protoLoader.loadSync(PROTO_PATH, { keepCase: true, longs: String, enums: String, defaults: true, }); const protoDescriptor = grpc.loadPackageDefinition(protoDefinition); // Create client with Test Service defined in protobuf const client = new protoDescriptor.test.TestService( "localhost:50051", grpc.credentials.createInsecure(), ); // Call gRPC method client.test(null, () => { console.log("Server responded"); });
- This code creates a gRPC client by loading the test.proto service definition and connecting to a server running on localhost:50051.
- The client calls the test method of the TestService, and logs "Server responded" when the server returns a response.
Caveats
Text indexing search and geospatial indexing search in the same query are not supported in MongoDB. To do so, you need to use regular expressions to search text along with geolocation queries.
Conclusion
Geo-indexing within MongoDB unleashes the latent power of geospatial data, rendering it an invaluable asset for developers immersed in location-based services or any platform leveraging geographic information. Through adept comprehension and deployment of appropriate geo-indexing techniques, developers can elevate their applications’ efficiency and deliver enhanced user experiences. Whether embarking on a new application endeavor or seeking to optimize an ongoing project, integrating MongoDB’s geo-indexing capabilities facilitates seamless handling of intricate spatial queries.
REFERENCES AND FURTHER READING
- mongodb documentation