If you have a hidden index in MongoDB, you can use the unhideIndex()
method or the collMod
administration command to unhide it.
Tag: indexes
Fix “longitude/latitude is out of bounds” in MongoDB when Creating a 2dsphere Index
If you’re getting a “longitude/latitude is out of bounds” error when trying to create a 2dsphere index in MongoDB, it could be due to your longitude and latitude coordinates being in the wrong order.
Continue readingHow to Check Whether an Index is Hidden in MongoDB
From MongoDB 4.4, it’s possible to hide an index from the query planner. This allows you to evaluate the potential impact of dropping an index without actually dropping the index.
You can use the getIndexes()
method to check whether or not an index is hidden. If an index is hidden, that index will display a hidden
field as having a value of true
(i.e. "hidden" : true
).
3 Ways to Hide an Index from the Query Plan in MongoDB
Starting from MongoDB 4.4, we can now hide indexes from the query plan. This enables us to evaluate the potential impact of dropping an index without actually dropping the index.
If hiding it has a negative impact, we can unhide the index. This saves us from having to drop the index, then recreate it again.
Below are 3 ways to hide an index in MongoDB.
Continue reading3 Ways to Drop an Index in MongoDB
MongoDB provides several ways to drop an index or indexes.
To drop a single index, you can use the dropIndex()
method.
To drop multiple indexes, you can use either the dropIndexes()
method or the dropIndexes
administration command.
Find Out if a Query uses an Index in MongoDB
In MongoDB, you can use the cursor.explain()
method or the db.collection.explain()
method to determine whether or not a query uses an index.
These methods enable you to view the query plan for the query, which includes whether or not it uses an index.
Continue readingFix “index name must be a string” when Dropping Multiple Indexes in MongoDB
If you encounter the “index name must be a string” error when dropping multiple indexes in MongoDB, it’s probably because you’re passing the specifications document instead of the name.
When you use the dropIndexes()
method or the dropIndexes
command to drop multiple indexes, you need to pass the index names (not the specifications documents) in an array.
Create a Multikey Index in MongoDB
In MongoDB, when you create an index on a field that holds an array, it’s automatically created as a multikey index.
Multikey indexes support efficient queries against array fields.
Multikey indexes can be created for arrays that hold scalar data (e.g. strings, numbers, etc) and nested documents.
Continue readingCreate a 2dsphere Geospatial Index for Spherical Queries in MongoDB
MongoDB provides the following geospatial index types that support geospatial queries.
- 2d indexes support queries that calculate geometries on a two-dimensional plane.
2dsphere
indexes support queries that calculate geometries on an earth-like sphere.
In this article, I create a 2dsphere
index.
How to Create an Index with a Specific Name in MongoDB
When you create an index in MongoDB, if you don’t specify a name for it, MongoDB will assign one.
The default name consists of each indexed field name concatenated with the key value for that field. For example, the sort order, or if it’s a text
index, the string _text
, or if it’s a 2dsphere
index, the string _2dsphere
, etc.
But you can assign your own name when creating indexes in MongoDB.
Continue reading