MCQs Of Day 16: MongoDB Geospatial Indexing

Rashmi Mishra
0

 

MCQs Of Day 16: MongoDB Geospatial Indexing

1. What type of index must be created to perform geospatial queries in MongoDB?

  • A) 2d Index
  • B) 2dsphere Index
  • C) Text Index
  • D) Hashed Index

Answer: B) 2dsphere Index
Explanation: The 2dsphere index is used in MongoDB for geospatial queries that work with spherical geometry, such as querying points on a globe (latitude/longitude coordinates).


2. Which MongoDB operator is used to query documents within a specified radius of a point?

  • A) $geoNear
  • B) $geoWithin
  • C) $near
  • D) $geoIntersects

Answer: C) $near
Explanation: The $near operator is used to query for documents that are closest to a given point. It can be used to find the nearest documents to a specific location.


3. What is the correct GeoJSON type to represent a location with latitude and longitude?

  • A) Polygon
  • B) Point
  • C) LineString
  • D) MultiPoint

Answer: B) Point
Explanation: GeoJSON uses the Point type to represent a location defined by latitude and longitude coordinates.


4. Which MongoDB operator is used to find documents within a polygon?

  • A) $near
  • B) $geoWithin
  • C) $centerSphere
  • D) $geoIntersects

Answer: B) $geoWithin
Explanation: The $geoWithin operator is used to query for documents that lie within a specific geographical shape, such as a polygon.


5. What is the purpose of the $geoIntersects operator in MongoDB?

  • A) To find documents within a specified radius
  • B) To find documents that intersect with a specified geometry
  • C) To calculate distances between documents
  • D) To find documents within a circle

Answer: B) To find documents that intersect with a specified geometry
Explanation$geoIntersects is used to query for documents that intersect with a given geometry (e.g., a polygon or a circle).


6. Which of the following is the correct syntax to create a 2dsphere index on the location field?

  • A) db.locations.createIndex({ location: "2d" })
  • B) db.locations.createIndex({ location: "2dsphere" })
  • C) db.locations.createIndex({ location: 2dsphere })
  • D) db.locations.createIndex({ location: "geo" })

Answer: B) db.locations.createIndex({ location: "2dsphere" })
Explanation: The correct syntax to create a 2dsphere index is createIndex({ location: "2dsphere" }).


7. What is the correct unit for specifying distance in MongoDB geospatial queries using $nearSphere?

  • A) Meters
  • B) Kilometers
  • C) Miles
  • D) Radians

Answer: A) Meters
Explanation: When using $nearSphere, the distance is specified in meters.


8. Which MongoDB operator is used to query for documents within a specific area defined by a circle?

  • A) $nearSphere
  • B) $geoWithin
  • C) $geoIntersects
  • D) $centerSphere

Answer: D) $centerSphere
Explanation: The $centerSphere operator is used to query for documents within a specified circular region, defined by a center point and radius.


9. Which of the following is the correct way to represent a polygon in GeoJSON format in MongoDB?

  • A) type: "Polygon", coordinates: [[[]]]
  • B) type: "Polygon", coordinates: [[[],[]]]
  • C) type: "Polygon", coordinates: [[[[]]]]
  • D) type: "Polygon", coordinates: [[[lat, long], [lat, long]]]

Answer: D) type: "Polygon", coordinates: [[[lat, long], [lat, long]]]
Explanation: In GeoJSON format, a polygon is represented by an array of coordinates, where each coordinate pair defines a vertex of the polygon.


10. What type of geospatial queries can be performed with a 2dsphere index?

  • A) 2D plane geometry queries only
  • B) Spherical geometry queries, such as distance and location-based searches
  • C) Only proximity-based searches
  • D) Only within a square-shaped area

Answer: B) Spherical geometry queries, such as distance and location-based searches
Explanation: The 2dsphere index is used for spherical geometry queries, which can handle queries like distance-based searches on the Earth's surface.


11. What is the purpose of creating a geospatial index in MongoDB?

  • A) To perform text-based searches
  • B) To perform faster geospatial queries like distance and proximity searches
  • C) To perform full-text searches
  • D) To perform faster joins

Answer: B) To perform faster geospatial queries like distance and proximity searches
Explanation: Geospatial indexes enable faster querying of location-based data, such as finding nearby points or querying within a given radius.


12. Which MongoDB operator is used to query documents located within a specific GeoJSON geometry (e.g., a polygon or circle)?

  • A) $geoNear
  • B) $nearSphere
  • C) $geoWithin
  • D) $centerSphere

Answer: C) $geoWithin
Explanation: The $geoWithin operator is used to query documents located within a given GeoJSON geometry, such as a polygon or circle.


13. In MongoDB, what kind of data can be stored in a field with a 2dsphere index?

  • A) Only text data
  • B) Numerical data
  • C) GeoJSON objects representing points, lines, and polygons
  • D) Only date-time values

Answer: C) GeoJSON objects representing points, lines, and polygons
Explanation: A 2dsphere index allows storing and querying GeoJSON objects such as PointPolygon, and LineString, which represent geographical features.


14. What is the GeoJSON format used for?

  • A) Storing plain text data
  • B) Storing geographical location data in MongoDB
  • C) Representing file metadata
  • D) Representing data structures for NoSQL databases

Answer: B) Storing geographical location data in MongoDB
Explanation: GeoJSON is a format for encoding geographical data structures, such as points, lines, and polygons, which can be used in MongoDB for geospatial indexing and queries.


15. In MongoDB, which operator would you use to find the closest locations to a given point?

  • A) $geoIntersects
  • B) $nearSphere
  • C) $geoWithin
  • D) $near

Answer: D) $near
Explanation: The $near operator is used to find the closest documents to a specific point based on a 2dsphere index.


16. Which of the following MongoDB operators is used for searching within a specified radius using spherical geometry?

  • A) $geoNear
  • B) $geoWithin
  • C) $nearSphere
  • D) $centerSphere

Answer: C) $nearSphere
Explanation$nearSphere is used to find documents within a specified spherical radius, utilizing spherical geometry.


17. How is the radius specified in MongoDB geospatial queries?

  • A) In kilometers
  • B) In degrees
  • C) In meters
  • D) In miles

Answer: C) In meters
Explanation: When performing geospatial queries in MongoDB, the radius is typically specified in meters for proximity searches.


18. What type of geospatial data can be used in MongoDB?

  • A) Plain text data
  • B) JSON objects with geolocation coordinates
  • C) XML formatted data
  • D) Binary data for images

Answer: B) JSON objects with geolocation coordinates
Explanation: MongoDB uses GeoJSON objects (such as PointPolygonLineString) for geospatial data, which can be indexed using 2dsphere or 2d indexes.


19. Which operator allows you to query documents that are inside a specific polygon?

  • A) $geoNear
  • B) $geoWithin
  • C) $near
  • D) $centerSphere

Answer: B) $geoWithin
Explanation: The $geoWithin operator is used to query for documents within a specified geometry, such as a polygon.


20. What is the main difference between the $near and $nearSphere operators in MongoDB?

  • A) $near uses flat geometry, while $nearSphere uses spherical geometry
  • B) $near is only for polygons, while $nearSphere is only for points
  • C) $nearSphere can only be used with a 2d index
  • D) $near and $nearSphere are identical

Answer: A) $near uses flat geometry, while $nearSphere uses spherical geometry
Explanation: The $near operator uses flat geometry for distance calculations, while $nearSphere calculates distances based on spherical geometry (i.e., on the Earth's surface).


These questions cover a variety of essential MongoDB concepts related to geospatial indexing and queries, including syntax, operators, and their proper use. The explanations ensure that students understand both the "how" and "why" behind the answers.

You said:

rest 30

ChatGPT said:

ChatGPT

Here are the remaining 30 MCQs on MongoDB Geospatial Indexing with explanations:


21. What type of geospatial index is suitable for spherical (Earth-like) geometries?

  • A) 2d Index
  • B) 2dsphere Index
  • C) Hash Index
  • D) Text Index

Answer: B) 2dsphere Index
Explanation: The 2dsphere index is used to handle spherical geometries like Earth, supporting queries that calculate distances and locations on a sphere.


22. Which of the following is NOT a valid GeoJSON geometry type?

  • A) Point
  • B) Polygon
  • C) Circle
  • D) LineString

Answer: C) Circle
Explanation: GeoJSON does not define a Circle geometry type. You can represent circles by defining them as polygons or use the $centerSphere operator.


23. What is the default distance unit for MongoDB geospatial queries?

  • A) Kilometers
  • B) Miles
  • C) Meters
  • D) Feet

Answer: C) Meters
Explanation: The default unit for specifying distances in MongoDB geospatial queries is meters.


24. What is the correct syntax for querying documents within a 10 km radius of a given point using $nearSphere?

  • A) { location: { $near: [10, 20] } }
  • B) { location: { $nearSphere: { $geometry: { type: "Point", coordinates: [10, 20] }, $maxDistance: 10000 } } }
  • C) { location: { $near: { coordinates: [10, 20], radius: 10000 } } }
  • D) { location: { $geoWithin: { $centerSphere: [[10, 20], 10] } } }

Answer: B) { location: { $nearSphere: { $geometry: { type: "Point", coordinates: [10, 20] }, $maxDistance: 10000 } } }
Explanation: The $nearSphere operator is used to query documents within a specified spherical radius (in meters), which is 10,000 meters (10 km) in this case.


25. Which operator is used to find all the documents that intersect a specified GeoJSON geometry?

  • A) $near
  • B) $geoIntersects
  • C) $geoWithin
  • D) $nearSphere

Answer: B) $geoIntersects
Explanation: The $geoIntersects operator is used to find documents that intersect a specified geometry (e.g., polygon or point).


26. In MongoDB, which index is used for handling location-based queries on a flat 2D plane (non-spherical)?

  • A) 2d Index
  • B) 2dsphere Index
  • C) Hashed Index
  • D) Text Index

Answer: A) 2d Index
Explanation: The 2d index is used for geospatial queries on a flat 2D plane (e.g., Cartesian coordinates) rather than spherical geometry.


27. What does the GeoJSON Polygon object represent in MongoDB?

  • A) A single point in space
  • B) A line between two points
  • C) A multi-point shape
  • D) A closed shape defined by an array of coordinates

Answer: D) A closed shape defined by an array of coordinates
Explanation: A GeoJSON Polygon is a closed shape defined by an array of coordinates that represent the vertices of the polygon.


28. What does $centerSphere represent in a geospatial query?

  • A) A spherical area defined by a center and radius in meters
  • B) A polygon area
  • C) A flat circle on a 2D plane
  • D) A query that finds the nearest points

Answer: A) A spherical area defined by a center and radius in meters
Explanation: The $centerSphere operator allows querying within a spherical area based on a center point and a radius, with the radius specified in meters.


29. In MongoDB, which type of geospatial query allows searching for all points within a given distance from a specified location?

  • A) $near
  • B) $geoIntersects
  • C) $nearSphere
  • D) $geoWithin

Answer: C) $nearSphere
Explanation: The $nearSphere operator allows searching for points within a specified distance from a given location using spherical geometry.


30. What should be done before performing any geospatial queries in MongoDB?

  • A) Create a 2d index
  • B) Create a 2dsphere index
  • C) Insert documents with non-GeoJSON data
  • D) Create a normal text index

Answer: B) Create a 2dsphere index
Explanation: Before performing geospatial queries with spherical geometry, a 2dsphere index must be created on the location field.


31. Which of the following is a valid GeoJSON Point object?

  • A) { "type": "Point", "coordinates": [30, 10] }
  • B) { "type": "Point", "coordinates": [10] }
  • C) { "type": "Point", "coordinates": [10, 20, 30] }
  • D) { "type": "Point", "coordinates": ["10", "20"] }

Answer: A) { "type": "Point", "coordinates": [30, 10] }
Explanation: The Point GeoJSON object must have two values: latitude (y) and longitude (x), and they should be in the correct order: [longitude, latitude].


32. Which operator is used in MongoDB to query documents that are within a polygon?

  • A) $geoIntersects
  • B) $near
  • C) $geoWithin
  • D) $centerSphere

Answer: C) $geoWithin
Explanation: The $geoWithin operator allows querying for documents that lie within a specific GeoJSON geometry, like a polygon.


33. How would you perform a geospatial query to find locations near the city center using $nearSphere?

  • A) db.locations.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [0, 0] }, $maxDistance: 5000 } } })
  • B) db.locations.find({ location: { $geoWithin: { $centerSphere: [[0, 0], 5000] } } })
  • C) db.locations.find({ location: { $near: { coordinates: [0, 0], radius: 5000 } } })
  • D) db.locations.find({ location: { $geoIntersects: { $geometry: { type: "Point", coordinates: [0, 0] } } } })

Answer: A) db.locations.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [0, 0] }, $maxDistance: 5000 } } })
Explanation: The $nearSphere operator is used to query for documents within a specified spherical radius. The coordinates are specified as [longitude, latitude], and $maxDistance is in meters.


34. What is the correct syntax for a LineString in GeoJSON format?

  • A) type: "LineString", coordinates: [[lat1, long1], [lat2, long2]]
  • B) type: "LineString", coordinates: [[long1, lat1], [long2, lat2]]
  • C) type: "LineString", coordinates: [[lat1, long2], [lat3, long3]]
  • D) type: "LineString", coordinates: [lat1, long1]

Answer: B) type: "LineString", coordinates: [[long1, lat1], [long2, lat2]]
Explanation: A LineString in GeoJSON format is defined by an array of coordinates, where each coordinate is specified as [longitude, latitude].


35. When using $nearSphere in MongoDB, what is the default behavior regarding the sort order of the results?

  • A) Results are sorted by distance, nearest first
  • B) Results are sorted by latitude
  • C) Results are sorted by longitude
  • D) Results are sorted by name

Answer: A) Results are sorted by distance, nearest first
Explanation: By default, MongoDB sorts the results of $nearSphere queries by distance, with the nearest documents coming first.


36. What is the result of a geospatial query using $geoWithin and $centerSphere in MongoDB?

  • A) All documents that are within a polygon
  • B) All documents that intersect a specified geometry
  • C) All documents within a circular region defined by a center and radius
  • D) All documents within a specified distance from a point

Answer: C) All documents within a circular region defined by a center and radius
Explanation: The $geoWithin operator in combination with $centerSphere finds documents within a specified circular region based on a center point and radius.


37. Which MongoDB operator is used to find documents within a specified polygon geometry?

  • A) $geoIntersects
  • B) $geoWithin
  • C) $nearSphere
  • D) $near

Answer: B) $geoWithin
Explanation: The $geoWithin operator is used to find documents within a specified GeoJSON geometry such as a polygon.


38. Which query returns documents located inside a specified polygon?

  • A) { location: { $nearSphere: [10, 20] } }
  • B) { location: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [[...]] } } } }
  • C) { location: { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [[...]] } } } }
  • D) { location: { $geoWithin: { $centerSphere: [[10, 20], 5] } } }

Answer: B) { location: { $geoWithin: { $geometry: { type: "Polygon", coordinates: [[...]] } } } }
Explanation: This query uses $geoWithin with a Polygon geometry to find all documents located inside the polygon.


39. Which operator would you use to find the nearest restaurants to a given location?

  • A) $geoWithin
  • B) $near
  • C) $centerSphere
  • D) $geoIntersects

Answer: B) $near
Explanation: The $near operator is used to find the nearest points to a specified location, often used in restaurant or location-based queries.


40. How do you represent a multi-point GeoJSON geometry in MongoDB?

  • A) { type: "MultiPoint", coordinates: [[lat1, long1], [lat2, long2]] }
  • B) { type: "Point", coordinates: [[lat1, long1], [lat2, long2]] }
  • C) { type: "MultiPoint", coordinates: [lat1, long1, lat2, long2] }
  • D) { type: "MultiPoint", coordinates: [[long1, lat1], [long2, lat2]] }

Answer: A) { type: "MultiPoint", coordinates: [[lat1, long1], [lat2, long2]] }
Explanation: A MultiPoint GeoJSON geometry is represented by an array of points, where each point is specified as [latitude, longitude].


41. Which MongoDB operator is used to perform a geospatial query to find all documents within a specific area using a polygon?

  • A) $geoWithin
  • B) $near
  • C) $centerSphere
  • D) $geoIntersects

Answer: A) $geoWithin
Explanation: The $geoWithin operator is used to find all documents within a specified GeoJSON polygon.


42. What is the main difference between $near and $nearSphere in MongoDB?

  • A) $near uses flat geometry, while $nearSphere uses spherical geometry
  • B) $nearSphere is faster than $near
  • C) $near is used for polygons, while $nearSphere is for points
  • D) $near works with 3D locations, while $nearSphere works with 2D

Answer: A) $near uses flat geometry, while $nearSphere uses spherical geometry
Explanation$near uses flat geometry, suitable for 2D calculations, while $nearSphere uses spherical geometry for distance calculations.


43. Which operator is used to find all documents that lie within a circular area?

  • A) $near
  • B) $geoWithin
  • C) $centerSphere
  • D) $geoIntersects

Answer: C) $centerSphere
Explanation: The $centerSphere operator is used to query for documents that are within a circular area defined by a center point and radius.


44. What type of geospatial index is created for queries that use the $geoWithin operator with a polygon?

  • A) 2d Index
  • B) 2dsphere Index
  • C) GeoJSON Index
  • D) Text Index

Answer: B) 2dsphere Index
Explanation: A 2dsphere index is required to query geospatial data with $geoWithin using GeoJSON geometries, including polygons.


45. In MongoDB, which operator allows finding documents that intersect a specific GeoJSON geometry (e.g., polygon)?

  • A) $geoIntersects
  • B) $geoWithin
  • C) $nearSphere
  • D) $centerSphere

Answer: A) $geoIntersects
Explanation: The $geoIntersects operator is used to find documents that intersect with a specific GeoJSON geometry (such as a polygon).


46. What kind of query can $geoIntersects be used for in MongoDB?

  • A) Finding all documents within a circle
  • B) Finding documents within a polygon
  • C) Finding documents within a rectangular area
  • D) Finding the nearest documents

Answer: B) Finding documents within a polygon
Explanation: The $geoIntersects operator is used for queries to find documents that intersect a specific geometry, such as a polygon.


47. What happens when you query using $near on a 2d indexed field?

  • A) It calculates the shortest path based on the 2D plane
  • B) It calculates distances based on spherical geometry
  • C) It ignores distance calculations
  • D) It only returns results within a polygon

Answer: A) It calculates the shortest path based on the 2D plane
Explanation: The $near operator on a 2d index calculates distances based on a flat 2D plane, typically used for Cartesian coordinates.


48. How does MongoDB store geospatial data for querying?

  • A) As plain text
  • B) Using GeoJSON format
  • C) Using XML format
  • D) As binary data

Answer: B) Using GeoJSON format
Explanation: MongoDB stores geospatial data in the GeoJSON format, which is a standard format for encoding geographic data structures.


49. Which of the following is a valid example of a GeoJSON Polygon?

  • A) { "type": "Polygon", "coordinates": [[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]] }
  • B) { "type": "Polygon", "coordinates": [[0, 0], [5, 5], [0, 5]] }
  • C) { "type": "Polygon", "coordinates": [[0, 0], [5, 5], [0, 5]] }
  • D) { "type": "Polygon", "coordinates": [[[0, 0], [0, 5], [5, 0]]] }

Answer: A) { "type": "Polygon", "coordinates": [[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]] }
Explanation: A valid Polygon in GeoJSON format is defined by an array of coordinates, which must be a closed loop. This answer follows the correct structure for a polygon.


50. What type of query does $nearSphere support in MongoDB?

  • A) Queries based on polygon intersections
  • B) Queries based on proximity to a point on a sphere
  • C) Queries for locations within a flat plane
  • D) Queries for documents within a rectangular area

Answer: B) Queries based on proximity to a point on a sphere
Explanation: The $nearSphere operator supports geospatial queries based on proximity to a point on a spherical surface, often used for distance calculations on Earth.


Tags

Post a Comment

0Comments

Post a Comment (0)

About Me