Stored procedures are used in relational databases to ensure that complex query operations are executed at the database layer. Nowadays, the need for stored procedures is replaced by other more advanced techniques, such as the aggregation pipelines available in MongoDB.
A stored procedure is SQL code stored on a database for repeated usage. You can think of a stored procedure as equivalent to functions for databases. Stored procedures were typically used in RDBMS (relational database management systems), such as SQL Server, for complex queries that required access to multiple tables. However, modern databases such as MongoDB have solved this problem by using a document model.
MongoDB does not support stored procedures in the traditional sense. It does, however, offer different mechanisms to provide similar results. For high-performance queries that should be performed on the database server, MongoDB provides the aggregation pipelines framework. For functions that need to be stored on the server, you can use Atlas Functions. Those functions can then be used in HTTPS endpoints to create serverless functions or in Triggers to be automatically used under specific conditions.
In MongoDB, stored procedures are not supported like in relational databases. Instead, it offers other ways to palliate this need.
Stored procedures are often used when complex queries need to be performed on the database server. In MongoDB, this is where aggregation pipelines shine. With aggregation pipelines, you use a series of stages to build a sequence of tasks where the output of a stage is the input for the next one. The aggregation pipelines framework is a fully Turing complete programming language and can be used for many things, ranging from complex data analytics to mining bitcoins.
Aggregation pipelines can be called directly from your application using one of the native drivers to connect to your database.
The MongoDB Atlas web UI (user interface) provides an easy-to-use interface to learn how to build aggregation pipelines. Compass, the GUI for MongoDB, also offers an interface to build pipelines. It even includes previews at each of the stages to better understand how to optimize your queries.
The Practical MongoDB Aggregation book is a valuable resource for learning more about aggregation pipelines and how to use them optimally.
When functions need to be stored on the database server, you can use Atlas Functions. You can then invoke these functions from the Realm via HTTPS endpoints or Atlas Triggers. Using functions is a straightforward way to implement a serverless architecture directly from the MongoDB Atlas data platform.
Because functions are running on the database server, they provide low-latency access to the data. These functions are often used when details of an implementation need to be abstracted away from the client application.
MongoDB does not have stored procedures. Instead, you can use aggregation pipelines or Realm functions to get the same results you would with a stored procedure.