DEV Community

Cover image for Delightful Database Design - Mastering MongoDB Queries - Series #11
Functional Javascript
Functional Javascript

Posted on

2

Delightful Database Design - Mastering MongoDB Queries - Series #11

Legend

AF = Aggregation Framework

Intro

We've covered a lot of powerful features of shaping data that we retrieve from the database, but we've also barely scratched the surface.

Other powerful data manipulation actions we can do:

  • joins (using the $lookup operator)
  • writing results to new collections (using the $out operator)
  • performing unions on dataset (using the $unionAll operator)
  • merging datasets (using the $merge operator)

These are all "stage operators". They are stages of our aggregation pipeline, which is our full query, and is represented as an arr of stages. Each stage is a single atomic unit of our database query.

Here is some pseudocode to understand the structure of composing the database query:

const aggregationPipeline = [
 { $stage1 },
 { $stage2 },
 { $stage3 },
]
Enter fullscreen mode Exit fullscreen mode

Then we simply make the call to the database using the "aggregate" func.
The aggregate func takes one parameter, the aggregation pipline arr; which is simply an arr of objs called stages.
That's all there is to the "MongoDB Aggregation Framework" (AF) from the bird's eye view:

return await client.db(dbName).collection(collName).aggregate(aggregationPipeline).toArray();
Enter fullscreen mode Exit fullscreen mode

Question and Answer

1.
How does AF compare with other database query systems, like Mongoose or GraphQL.

AF vs Mongoose:

The AF is well beyond the capability of Mongoose.
When you're composing AF queries, you're writing queries directly for the MongoDB engine and optimizer. It natively understands the query and optimizes it. AF is the future direction of the MongoDB company and ecosystem. They are pouring tens of thousands of development and research hours into it to make it better every iteration.

AF vs GraphQL:

These are two different products.
For example, your GraphQL resolvers can use the AF to retrieve data. GraphQL has it's set of usecases that it is designed for, like bringing various (or disparate) datastores together into a coherent and consistent orchestration.
AF is deceptively simple; But also deceptively powerful and performant. So unless your enterprise usecase calls for a GraphQL-style solution, you may be better off selecting the least amount of moving parts.

What's Next?

In the next few series of articles, we'll do some advanced and creative data-shaping activities. Taking the AF approach makes this possible.

DevCycle image

OpenFeature Multi-Provider: Enabling New Feature Flagging Use-Cases

DevCycle is the first feature management platform with OpenFeature built in. We pair the reliability, scalability, and security of a managed service with freedom from vendor lock-in, helping developers ship faster with true OpenFeature-native feature flagging.

Watch Full Video πŸŽ₯

Top comments (0)

Image of Timescale

PostgreSQL for Agentic AI β€” Build Autonomous Apps on One Stack ☝️

pgai turns PostgreSQL into an AI-native database for building RAG pipelines and intelligent agents. Run vector search, embeddings, and LLMsβ€”all in SQL

Build Today

DEV Launches and Announcements

🐯 πŸš€ Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

TL;DR: Eight years ago, we launched Timescale to bring time-series to PostgreSQL. Our mission was simple: help developers building time-series applications.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❀️