Here are a few best practices you can follow to optimise your MongoDB performance :
Queries on MongoDB work faster if there are indexed fields. You can use indexes to make data retrieval faster by reducing the number of disk I/Os.
By default the _id field is indexed. But you can index other fields as well. For example, if you sort your query results in the ascending/descending order of dateAdded you can create an index on this field. In Mongoose you will do something like this to create an index :
{
...
dateAdded : {type : 'Date', index : true}
}
Make sure you turn off autoIndex in production mode :
schema.set('autoIndex', false);
A separate data structure is maintained by MongoDB when you create an index. This consumes more memory. So, you need to be careful with indexes and avoid using them excessively.
Check out the docs to learn more about indexes.
Many times to do an update we retrieve a document, make the updates and then save the document. This can be slow in some cases. You can do faster updates by issuing updates to the modified fields only.
If you have deployed a replica set, read from primary. Although the secondary instances update themselves quickly, the reads on secondary may not be consistent with primary instance due to network latency.
Store all the data for a particular record in a single document. This speeds up retrieval because all the required data is retrieved in a single query. For example, in your post schema you can have embedded comments so that when you retrieve a post all the comments are automatically fetched.
But there are cases when this is not possible. For instance, you might have a posts collection and each post is owned by an author. So, you need to link each post with an author from authors collection. Here, you can't have an embedded author. So, you need to store the ref in post document and make another query to populate it while retrieving the post. This can be done easily with populate() in Mongoose.
MongoDB stores most frequently accessed documents in RAM in order to have faster retrieval. So, while deploying a MongoDB server make sure you have enough RAM. If possible deploy Redis and MongoDB servers separately in different machines with sufficient RAM.
As field names are repeated across documents, they consume memory. By keeping field names shorter you can save some memory and more documents will fit into the RAM.
These are some of the basic things you need to take care of to make MongoDB faster. Apart from this you can use explain feature to see how MongoDB executes your query, whether indexes are used or not etc. Also in my opinion if you have an option to choose between SSD and non-SSD cloud go for the SSD. This is because most of the disk I/Os are random in MongoDB and you will definitely have a performance boost by using SSDs.