Mongodb Certified Developer Associate C100dev · Free Practice Question Easy
Question 30
Suppose we have a shipwrecks collection with the following document structure:
- {
- _id: ObjectId("578f6fa2df35c7fbdbaed8ce"),
- feature_type: 'Wrecks - Visible',
- watlev: 'always dry',
- coordinates: [ -79.9469681, 9.3729954 ]
- }
Your application will often query based on the coordinates field. How to create a 2dsphere geospatial index for the coordinates field?
-
A
db.shipwrecks.createIndex({'coordinates': 'text'}) -
B
db.shipwrecks.createIndex({'coordinates': '2dsphere'}) -
C
db.shipwrecks.createIndex({'coordinates': '2d'}) -
D
db.shipwrecks.createIndex({'coordinates': '1'})
Reveal correct answer
Correct answer: B
A.
This option creates a text index, which is used for full-text search. It is not relevant for geospatial queries or working with coordinates, so it is not the correct choice here.
B.
This option correctly specifies the creation of a 2dsphere geospatial index on the coordinates field. The 2dsphere index is specifically designed to support geospatial queries on a sphere, such as querying for points within a certain radius or performing geometric operations. It is the correct choice when working with latitude and longitude coordinates.
C.
This option creates a 2d geospatial index, which is designed for use with flat (planar) coordinate systems. It is not suitable for working with latitude and longitude coordinates on a sphere, so it is not the correct choice in this scenario.
D.
This option specifies creating an index with a string value of '1'. It does not correspond to any specific index type, let alone a geospatial index. Therefore, it is not the correct choice in this context.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
