Making sure that you have indices on any fields you regularly query against Storing the results of frequent sub-queries on documents to reduce read load Other ways to improve MongoDB performance after identifying your major query patterns include: With MongoDB, developers have access to several powerful tools that can help them greatly improve performance-but that doesn’t mean that query patterns and profiles can be ignored, either.įor example, one easy way to beef up performance is by simply analyzing your query patterns and determining where you can embed data rather than doing in-application or in-database joins. Once you know your application’s query patterns like the back of your hand, you can design your data model and select appropriate indices accordingly. Most developers would agree that the first step in optimizing performance is to understand expected and actual query patterns. Without further ado, let’s go over the top 5 best practices for getting peak performance out of your MongoDB databases.īest Practice #1: Examine query patterns and profiling MongoDB Atlas can handle federated queries across object storage (e.g., Amazon S3) and document storage. You can use MongoDB Atlas for auto-scaling. Of course, the exact answer depends on your data size and your workloads. MongoDB needs enough RAM to hold your working set in memory. And, if you haven’t, definitely try sharding for horizontal scaling. Don’t do joins (embedding is preferable). It depends on what you are and aren’t doing already. Queries without indexes depend on collection size and machine specs, etc. Primary key or index queries should take just a few milliseconds. MongoDB is a distributed database by default, which allows for expansive horizontal scalability without any changes to application logic. If you only have a few minutes to spare, this quick performance FAQ might be useful:Īd hoc queries, indexing, and real time aggregation provide powerful ways to access data. Running MongoDB on Atlas, the fully managed, global cloud database service Starting your first project as a seasoned MongoDB developer. While anyone interested in document database platforms could learn something from this post, you’ll probably find this information particularly useful if you’re: Please keep in mind that the best practices we are going to cover are not exhaustive (that would require a much longer post).
#Mongo db optimizer how to#
In this post, we’ll focus on how to achieve performance at scale using MongoDB by looking at: Performance issues may indicate that the database is not working as hard as it could and that specific optimizations could lead to better performance. With its JSON-like documents, MongoDB is notable for horizontal scaling and load balancing, which offers developers an excellent balance of customization and scalability.īut like any high-performance tool, MongoDB performs best in the hands of an expert who knows what they’re doing. Ready for more? Check out these advanced aggregation examples with $lookup.MongoDB is the premier NoSQL document database for modern developers working on high-performance applications. With built in optimization, the pipeline makes it easy to progress data through different stages to compute results with native Mongo functionality. The aggregation pipeline is the preferred method for aggregating data in MongoDB. You can work around this limit with the allowDiskUse option, otherwise Mongo will throw an error. Additionally, each stage is limited to 100 megabytes of RAM. The end result has the same size limitations per document (16 megabytes). The aggregation pipeline either returns results in a cursor or stores the results in a collection. $unwind takes each of these string values and outputs a new document with the parent document's data. The tags field for each user is an array of string values. $unwind deconstructs an array field and generates an output document for each element in the array. A Quick Exampleįor example, let's say you have a collection "posts". While Mongo also supports map-reduce and single purpose aggregation methods, the aggregation pipeline is the preferred method for data aggregation in MongoDB. Using the aggregation pipeline, you can process multiple documents to return a computed result. The aggregation pipeline is used to process and aggregate data from collections.