Skip to main content

πŸ‘ Writing long pipelines

Aggregation pipelines can get very long, depending on how many stages we need to run. Writing a pipeline is writing code, as you will write it using one of the many MongoDB drivers in your own language. Here we're presenting the examples using JavaScript suitable for the MongoDB Shell mongosh, but if you are writing a microservice in Rust, you'll definitely write your pipelines in Rust.

danger

The following syntax doesn't work in the Atlas UI aggregations editor. The editor doesn't support declaring variables.

This is why we should rewrite our last pipeline like this:

Get 15 books from 1985 with 150 pages. Show only the title, year, totalInventory and available books. (Sample doc here)

db.books.aggregate([
{$match: {year: 1985, pages: 150}}, {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}}, {$limit: 15}
])

Will be changed into:

let booksFrom1985With150pages = {$match: {year: 1985, pages: 150}};
let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}};
let getJust15books = {$limit: 15};

db.books.aggregate([
booksFrom1985With150pages,
showOnlyTheseFields,
getJust15books,
]);

Easier to read, and reason about, right?

πŸ‘ Try to run the above pipeline and compare your results: should be the same as before

tip

Write your aggregation pipelines like you'll compose functions in your programming language. Aggregations are code that runs on the server. In the client you just express what you want to be done, not how to do it.

tip

As this is code, we can even add comments (starting with //) to our pipelines

πŸ‘ We can also use $gte to get the books with 150 pages or more. Check $gte syntax in the docucumentation and write an aggregation pipeline to return 15 books from 1985 with more than 150 pages. Show only the title, year, totalInventory and available books. (Sample doc here)

Hint: we'll need to use $and as maybe this is a bit more complex.

Answer
let moreThan150pages =  {pages: {$gte: 150}}
let booksFrom1985WithMoreThan150pages = {$match: {$and: [{year: 1985}, moreThan150pages]}};
let showOnlyTheseFields = {$project: {_id: 0, title: 1, year: 1, totalInventory: 1, available: 1}};
let getJust15books = {$limit: 15};

db.books.aggregate([
booksFrom1985WithMoreThan150pages,
showOnlyTheseFields,
getJust15books,
]);