Fix “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.

Continue reading

5 Ways to Get the Hour from a Date in MongoDB

MongoDB provides a good range of aggregation pipeline operators for working with dates, including operators that extract certain parts of dates, such as the year, month, day, hours, minutes, etc.

There are also a couple of MongoDB methods that enable you to iterate through a cursor, and apply a JavaScript function. You can therefore use JavaScript to extract date values and date parts, etc from a field as required.

This article presents 5 ways to return the hour portion from a Date object in MongoDB.

Continue reading