Skip to content

Commit

Permalink
Add Atlas UI sections
Browse files Browse the repository at this point in the history
  • Loading branch information
sis0k0 committed Dec 11, 2023
1 parent bbedb5a commit b14d995
Show file tree
Hide file tree
Showing 15 changed files with 379 additions and 52 deletions.
4 changes: 2 additions & 2 deletions docs/1-mongodb-atlas/15-prerequisites.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ description: Setting up your MongoDB Atlas account, importing Library data

To follow along you'll need:
- a MongoDB Atlas account
- some test data, in this case this is Books, Authors and Reviews data for a Library management system.
- test data. In this case, this is books, authors and reviews data for a library management system.

💻 To get both, open the [Intro Lab](https://mongodb-developer.github.io/intro-lab/docs/intro) and follow it (only takes 10-15 mins) to get your database ready. Return here when finished!
💻 To get both, open the [Intro Lab](https://mongodb-developer.github.io/intro-lab/docs/intro) and follow it (only takes 10-15 mins) to get your database ready. Return here when finished!
39 changes: 21 additions & 18 deletions docs/30-simple-queries/0-using-library-database.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ import TabItem from '@theme/TabItem';

## Select the library database

💻 We'll use the `library` database. To do that, in a MongoDB shell type in:
💻 We'll use the `library` database for all of the hands-on exercises in this lab.
If you haven't already, import the [library data](https://mdb.link/import-library-data) into your database cluster.

<Tabs groupId="aggregations">
<TabItem value="mongosh" label="mongosh">
<TabItem value="atlas" label="Atlas UI">

Select the correct database in the Atlas UI.


<Screenshot src="/img/30-simple-queries/select-db.png" url="http://cloud.mongodb.com/yourcluster" alt="AtlasUI Showing the available DBs" />

</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">

To do that, in a MongoDB shell type in:

```js
use library
Expand All @@ -20,26 +31,12 @@ You can show all collections with
show collections
```

</TabItem>
<TabItem value="atlas" label="Atlas UI">

Select the correct DB in the Atlas UI


<Screenshot src="/img/30-simple-queries/select-db.png" url="http://cloud.mongodb.com/yourcluster" alt="AtlasUI Showing the available DBs" />

</TabItem>
</Tabs>



---


🦸‍♂️ 💻 How would you switch to a database called `orders`?

:::info
Extra activity, do it if you have extra time or are following at home, won't be covered during the hands-on Lab
Extra activity, do it if you have extra time or are following at home, won't be covered during the hands-on lab.
:::

<details>
Expand Down Expand Up @@ -70,4 +67,10 @@ You can also list other databases in your MongoDB instance with

```js
show databases
```
```


</TabItem>

</Tabs>

29 changes: 15 additions & 14 deletions docs/30-simple-queries/1-empty-aggregation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,28 @@ import TabItem from '@theme/TabItem';
This code is the equivalent to a `SELECT * FROM AUTHORS`. Returns a [cursor](https://www.mongodb.com/docs/manual/reference/method/js-cursor/) with all documents in the `authors` collection:

<Tabs groupId="aggregations">
<TabItem value="mongosh" label="mongosh">

```js
db.authors.aggregate([])
```
<TabItem value="atlas" label="Atlas UI">

</TabItem>
<TabItem value="atlas" label="Atlas UI">
<Screenshot src="/img/30-simple-queries/atlas-aggregation.png" url="http://cloud.mongodb.com/" alt="Atlas UI showing an empty aggregation pipeline" />

<Screenshot src="/img/30-simple-queries/atlas-aggregation.png" url="http://cloud.mongodb.com/yourcluster" alt="AtlasUI Showing the available DBs" />

- Open the Aggregation tab
- Select Text entry
- Type in the aggregation pipeline:
- Open the `Aggregation` tab.
- Select `Text`.
- Notice the empty array in the editor denoting an empty aggregation pipeline:

```js
[]
```

</TabItem>
</Tabs>

<TabItem value="mongodb-shell" label="MongoDB Shell">

```js
db.authors.aggregate([])
```

We can iterate over this cursor and get more documents typing `it`.
We can iterate over the returned cursor and get more documents typing `it`.

💻 Return all the documents in the `books` collection and iterate to get the next page of books.

Expand Down Expand Up @@ -70,4 +67,8 @@ cursor.itcount()
db.books.aggregate([]).itcount()
```
</div>
</details>
</details>

</TabItem>

</Tabs>
160 changes: 154 additions & 6 deletions docs/30-simple-queries/2-match.mdx
Original file line number Diff line number Diff line change
@@ -1,34 +1,115 @@
# $match
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

This is the simplest one, similar to the `WHERE` SQL clause.
# $match

Say we want all the books from 2010. We'll write:
The $match operator is used in conjunction with the aggregation framework to filter documents in a collection. It takes a document as input and returns a new document containing only the documents that match the specified criteria. The syntax for the $match operator is as follows:

```js
db.books.aggregate([{$match: {year: 2010}}])
{ $match: { <expression>: <value> } }
```

## Expressions

The `<expression>` portion of the $match operator 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

<Tabs groupId="aggregations-year">
<TabItem value="atlas" label="Atlas UI">

First, make sure you select the `books` collection in the Atlas UI.
<Screenshot src="/img/30-simple-queries/select-books-collection.png" url="http://cloud.mongodb.com/" alt="Atlas UI database deployment with the books collection highlighted." />

Then, navigate to the `Aggregation` tab and click `Add Stage`.
<Screenshot src="/img/30-simple-queries/new-aggregation.png" url="http://cloud.mongodb.com/" alt="Atlas UI database deployment with aggregation tab highlighted." />

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

```js
[
{
$match: { year: 2010 }
}
]
```

<Screenshot src="/img/30-simple-queries/match-screenshot.png" url="http://cloud.mongodb.com/" alt="Atlas AI $match aggregation." />

</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
Say we want all the books from 2010. We'll write:

```js
db.books.aggregate([{$match: {year: 2010}}])
```
</TabItem>
</Tabs>


💻 Return all the `books` that have exactly 100 pages.

<details>
<summary>Answer</summary>
<div>

<Tabs groupId="aggregations-pages">
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: { pages: 100 }
}
]
```

</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {pages: 100}}])
```
</TabItem>
</Tabs>


</div>
</details>

## AND

If we need to add more conditions using AND, we can do it using the `$and` operator.
If we need to add more conditions using AND, we can do it with the `$and` operator.

If we want all the books with 100 pages with exactly `totalInventory` 2 we can use an `$and` operator. This takes and array of documents with all the conditions that should be true for the AND to succeed:

<Tabs groupId="aggregations-and">
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: {
$and: [
{ pages: 100 },
{ totalInventory: 2 }
]
}
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {$and: [{pages: 100}, {totalInventory: 2}]}}])
```
</TabItem>
</Tabs>




The pseudo-code for this would be something like:

Expand All @@ -44,40 +125,107 @@ IF pages == 100 AND totalInventory == 2 {
<summary>Answer</summary>
<div>

<Tabs groupId="aggregations">
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: {
$and: [
{ pages: 100 },
{ year: 2015 }
]
}
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {$and: [{pages: 100}, {year: 2015}]}}])
```
</TabItem>
</Tabs>

</div>
</details>

💻 How many are they? (_hint: use `itcount()`_).
💻 How many are they?

<details>
<summary>Answer</summary>
<div>

<Tabs groupId="aggregations">
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: {
$and: [
{ pages: 100 },
{ year: 2015 }
]
}
},
{
$count: "books_count"
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {$and: [{pages: 100}, {year: 2015}]}}]).itcount()
```
</TabItem>
</Tabs>
</div>
</details>

### Shorthand AND

We can do an implicit AND just passing a document with all the conditions (instead of an array of documents):

<Tabs groupId="aggregations-shorthand-and">
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: {pages: 100, totalInventory: 2}
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {pages: 100, totalInventory: 2}}])
```
</TabItem>
</Tabs>


💻 Return all the `books` from 2015 that have exactly 100 pages, using the simple $and notation

<details>
<summary>Answer</summary>
<div>

<Tabs groupId="aggregations-shorthand-and-exercise">
<TabItem value="atlas" label="Atlas UI">
```js
[
{
$match: {pages: 100, year: 2015}
}
]
```
</TabItem>
<TabItem value="mongodb-shell" label="MongoDB Shell">
```js
db.books.aggregate([{$match: {pages: 100, year: 2015}}])
```
</TabItem>
</Tabs>
</div>
</details>
Loading

0 comments on commit b14d995

Please sign in to comment.