MongoDB Programming Tips

How to Get Sorted List of Collection and Index Sizes in a MongoDB Database

If you have a large MongoDB database in production with a large of number of collections and indexes, you may need to periodically analyse the size of them. Here are a few MongoDB script snippets that you may find useful. How to Get Sorted List of MongoDB Collections by Size? The following script sorts and […]

How to Get Distinct Keys of All Documents in a Collection in MongoDB

MongoDB is intended as a document storage database where we don’t know all the fields required in a stored entity when we build an application. This means that MongoDB collections can contain documents with different schemas. Some documents may have additional keys or different keys compared to other documents. This flexibility allows for system to […]

How to Get All Indexes in a MongoDB Database as a Script

When you have collections with large amount of data, it is very important to create proper indexes. MongoDB queries can be substantially faster with proper indexes created on collections to meet the application needs of data sorting and filtering. MongoDB automatically creates a uniq index on the _id field during the creation of the collection. […]

How to Create and Use Indexes in MongoDB Database

MongoDB supports creation of indexes to speed up queries for specific application needs. With suitable indexes, MongoDB doesn’t need to scan every document before returning results. This substantially speeds up queries especially for large collections where ordering and filtering is needed. MongoDB automatically creates a uniq index on the _id field during the creation of […]

How to Run Like Queries in MongoDB

When working with textual data in mongodb, you may want to fetch documents with partial match on a field. You can use the $regex operator in mongodb for running like queries. By default, the like query using $regex is case sensitive. Assume that you have collection in your mongodb containing names of the countries. A […]