π $match
The $match stage filters documents in a collection. It takes a document as input (your filter) and returns the documents that match the specified criteria. The syntax for the $match stage is as follows:
{ $match: { <expression>: <value> } }
Expressionsβ
The <expression> portion of the $match stage can be any valid MongoDB expression. This includes:
- Comparison operators:
eq,neq,gte,lte,gt,lt,in,nin,exists. - Regular expressions: regex.
- Logical operators: and, or, not.
- Subdocuments and arrays:
{ field: <value> }, [ <item>, <item>, ... ].
Matching book documentsβ
- Instruqt
- Atlas UI
- MongoDB Shell
Open the 100_aggregation_pipeline_match file in the Instruqt IDE or GitHub Codespaces. Run the Startup Code, that will import all the neccessary modules and connect to the local MongoDB cluster. Then, run the $match cell.

First, make sure you select the books collection in the Atlas UI.

Then, navigate to the Aggregation tab and click </> TEXT.

Say we want all the books from the year 2010. We can add a $match stage to filter the documents from the books collection:
[
{
$match: { year: 2010 }
}
]

Say we want all the books from 2010. We'll write:
db.books.aggregate([
{
$match: {year: 2010}
}
])
π $match Exercise: Return all the books that have exactly 100 pages.β
Answer
- Instruqt
- Atlas UI
- MongoDB Shell
Insert a new cell (you can do it by clicking the + Code button at the top left of the notebook) and write the following code:
const booksWith100pages = [
{ $match: {
pages: 100
}
}
];
const cursor = await books.aggregate( booksWith100pages );
await cursor.forEach((b) => {
console.log(b);
});
[
{
$match: { pages: 100 }
}
]
db.books.aggregate([
{
$match: { pages: 100 }
}
])
ANDβ
If we need to add more conditions using AND, we use the $and operator.
If we want all the books with 100 pages and with exactly totalInventory 1, we can use the $and operator. It takes an array of documents with all the conditions that should be true for the AND to succeed:
- Instruqt
- Atlas UI
- MongoDB Shell
Insert a new cell (you can do it by clicking the + Code button at the top left of the notebook) and write the following code:
const booksWith100pagesAndTotalInventory1 = [
{ $match: {
pages: 100,
totalInventory: 1
}
}
];
const cursor = await books.aggregate( booksWith100pagesAndTotalInventory1 );
await cursor.forEach((b) => {
console.log(b);
});
[
{
$match: {
$and: [
{ pages: 100 },
{ totalInventory: 1 }
]
}
}
]
db.books.aggregate([
{
$match: {
$and: [
{ pages: 100 },
{ totalInventory: 1 }
]
}
}
])
The pseudo-code for this would be something like:
IF pages == 100 AND totalInventory == 1 {
return matching docs
}
π Return all the books from 2015 that have exactly 100 pages.
Answer
- Instruqt
- Atlas UI
- MongoDB Shell
Insert a new cell (you can do it by clicking the + Code button at the top left of the notebook) and write the following code:
const booksWith100pagesFrom2015 = [
{
$match: {
$and: [
{ pages: 100 },
{ year: 2015 }
]
}
}
];
const cursor = await books.aggregate( booksWith100pagesFrom2015 );
await cursor.forEach((b) => {
console.log(b);
});
[
{
$match: {
$and: [
{ pages: 100 },
{ year: 2015 }
]
}
}
]
db.books.aggregate([
{
$match: {
$and: [
{ pages: 100 },
{ year: 2015 }
]
}
}
])
π How many are there? We haven't yet seen the $count stage, but try to add a second stage to your pipeline with { $count: "books_count" }.
Answer
- Instruqt
- Atlas UI
- MongoDB Shell
Insert a new cell (you can do it by clicking the + Code button at the top left of the notebook) and write the following code:
const countBooksWith100pagesFrom2015 = [
{
$match: {
$and: [
{ pages: 100 },
{ year: 2015 }
]
}
},
{
$count: "books_count"
}
];
const cursor = await books.aggregate( countBooksWith100pagesFrom2015 );
await cursor.forEach((b) => {
console.log(b);
});
[
{
$match: {
$and: [
{ pages: 100 },
{ year: 2015 }
]
}
},
{
$count: "books_count"
}
]
db.books.aggregate(
[
{ $match: {$and: [{pages: 100}, {year: 2015}]} },
{ $count: "books_count" }
]
)
Shorthand ANDβ
We can do an implicit AND just passing a document with all the conditions (instead of an array of documents):
- Instruqt
- Atlas UI
- MongoDB Shell
Insert a new cell (you can do it by clicking the + Code button at the top left of the notebook) and write the following code:
const booksWith100pagesWithInventory2 = [
{
$match: {
pages: 100,
totalInventory: 2 }
}
];
const cursor = await books.aggregate( booksWith100pagesWithInventory2 );
await cursor.forEach((b) => {
console.log(b);
});
[
{
$match: { pages: 100, totalInventory: 2 }
}
]
db.books.aggregate([
{
$match: { pages: 100, totalInventory: 2 }
}
])
π Return all the books from 2015 that have exactly 100 pages, using the shorthand $and notation:
Answer
- Instruqt
- Atlas UI
- MongoDB Shell
Insert a new cell (you can do it by clicking the + Code button at the top left of the notebook) and write the following code:
const booksWith100pagesFrom2015 = [
{
$match: {
pages: 100,
year: 2015
}
}
];
const cursor = await books.aggregate( booksWith100pagesFrom2015 );
await cursor.forEach((b) => {
console.log(b);
});
[
{
$match: {pages: 100, year: 2015}
}
]
db.books.aggregate([{$match: {pages: 100, year: 2015}}])