Skip to main content

πŸ¦Έβ€β™‚οΈ Writing Long Pipelines

info

Extra activity! Do it if you have extra time or are following along at home. It won't be covered during the hands-on lab.

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. You can try this using the built-in MongoDB Shell in MongoDB Compass.

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

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

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

Will be changed into:

let lessThan150 = { $lt: 150 };
let booksFrom1985With150pages = {$match: {year: 1985, pages: lessThan150 }};
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 understand, right?

πŸ‘ Try to run the above pipeline and compare your results. They 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. Or write functions that take parameters and return a stage. Or unit test our stages.

πŸ‘ 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).

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,
]);