MongoDB Programming Tips

MongoDB History

When it comes to modern web application development, MongoDB is the king. If you are a full stack programmer, you hear about MERN or MEAN stacks every day. The M is in every one of them and it stands for MongoDB. The free and open source community version of MongoDB powers a large number of […]

How to Find the Document with the Longest Array in a MongoDB Collection?

When working with MongoDB collections, sometimes you may want to find the document that contains the longest array. For example, you may have a collection for storing shirts and the array “sizes” in it may contain all the sizes in which the shirt is available. In this case, how do you find the shirts with […]

How to Find Largest Document in MongoDB

MongoDB collections are usually schema less. This means that a collection can potentially contain documents with different structure. Even if they are same, since data stored is different, document size can substantially vary. Sometimes you may want to find the largest document in a MongoDB collection. This is required if you are planning to migrate […]

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 […]